-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
1435 lines (1179 loc) · 51.8 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import datetime as dt
import os
import json
import logging
import random
from datetime import datetime, timedelta
import pytz
import asyncio
from discord.ui import Button, View
from dotenv import load_dotenv
from dateutil import parser as date_parser
import discord
from discord.ext import commands, tasks
from eliDb import initialize_database, add_member, add_cash, get_cash, add_xp, get_xp, reset_xp, remove_cash, remove_xp, get_xp_leaderboard, get_pet_details, update_pet_feedings, check_pet_health, adopt_pet, rename_pet, fetch_random_pet_from_store, add_shop_item, get_cash_leaderboard, deposit, withdraw, get_bank_balance, update_last_claim_times, get_last_claim_times, get_inventory, get_shop_items, add_shop_item, edit_shop_item, delete_shop_item, add_inventory_item, use_inventory_item
import traceback
import sqlite3
from flask import Flask
import threading
# Load environment variables from .env file
load_dotenv()
# Retrieve the token from environment variables
TOKEN = os.getenv('DISCORD_TOKEN_CHAI')
# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s:%(levelname)s:%(message)s')
# Function to load the prefix from the JSON file
def get_prefix():
try:
with open('variables.json', 'r') as f:
data = json.load(f)
return data.get('prefix', ';')
except (FileNotFoundError, json.JSONDecodeError):
return ';'
# Function to save the new prefix to the JSON file
def set_prefix(new_prefix):
try:
with open('variables.json', 'r') as f:
data = json.load(f)
except (FileNotFoundError, json.JSONDecodeError):
data = {}
data['prefix'] = new_prefix
with open('variables.json', 'w') as f:
json.dump(data, f, indent=4)
# Create an instance of a Client with the appropriate intents
intents = discord.Intents.default()
intents.guilds = True
intents.messages = True
intents.voice_states = True
intents.message_content = True
intents.members = True
# Initialize the bot with intents and dynamic command prefix
bot = commands.Bot(command_prefix=get_prefix(), intents=intents)
custom_color = discord.Color.from_rgb(179, 202, 25)
active_users = {}
#
#
#
#
#
#
#
@bot.command(description="Sends the bot's latency.")
async def ping(ctx):
latency = bot.latency * 1000 # Latency in milliseconds
embed = discord.Embed(title="Pong!", description=f"**Latency is {latency:.2f} ms**", color=custom_color)
await ctx.send(embed=embed)
# greeting
@bot.event
async def on_message(message):
if bot.user.mentioned_in(message) and ' say hi' in message.content.lower():
await message.channel.send('hi, i\'m plum')
minute_key = message.created_at.strftime("%Y-%m-%d %H:%M")
if minute_key not in active_users:
active_users[minute_key] = set()
active_users[minute_key].add(message.author.id)
logging.info(f"User {message.author.id} is active at {minute_key}")
await bot.process_commands(message)
# display user details
@bot.command(name='who')
async def who(ctx, member: discord.Member = None):
"""Display information about a member."""
if member is None:
member = ctx.author
user = await bot.fetch_user(member.id)
banner_url = user.banner.url if user.banner else None
embed = discord.Embed(title=f"{member.display_name}'s file", color=custom_color)
embed.set_thumbnail(url=member.avatar.url)
embed.add_field(name="Username", value=member.name, inline=True)
embed.add_field(name="Server Nickname", value=member.nick, inline=True)
embed.add_field(name="Display Name", value=member.display_name, inline=True)
embed.add_field(name="User ID", value=member.id, inline=True)
embed.add_field(name="Account Created", value=member.created_at.strftime("%Y-%m-%d %H:%M:%S"), inline=True)
embed.add_field(name="Joined Server", value=member.joined_at.strftime("%Y-%m-%d %H:%M:%S"), inline=True)
if banner_url:
embed.set_image(url=banner_url)
await ctx.send(embed=embed)
#
#
#
#
#
#
#
#
#
#
#
# level commands
# my xp command
@bot.command(name='myxp')
async def myxp(ctx, member: discord.Member = None):
"""Display XP"""
if member is None:
member = ctx.author
myxp = get_xp(member.id) # Pass member ID to get_xp
embed = discord.Embed(title=f"{member.display_name}'s experience", color=custom_color)
embed.add_field(name="Your XP", value=f":four_leaf_clover: {myxp}", inline=False)
await ctx.send(embed=embed)
# add xp
@bot.command(name='addxp')
async def addxp(ctx, xp: int = None, member: discord.Member = None):
"""Add xp to member"""
if not ctx.message.author.guild_permissions.administrator:
await ctx.send("You don't have permission to use this command.")
return
if member is None:
await ctx.send("Please mention a valid member.")
return
if xp is None:
await ctx.send("Please provide a valid amount of XP to add.")
return
try:
xp_int = int(xp)
except ValueError:
await ctx.send("Invalid amount. Please provide a valid integer.")
return
print(f"Adding {xp_int} XP to Member ID: {member.id}")
add_xp(member.id, xp_int)
logging.info(f"Added {xp_int} XP to Member ID: {member.id}")
await ctx.send(f"Added {xp_int} XP to {member.display_name}.")
# reset xp
@bot.command(name='ResetXp')
async def ResetXp(ctx, target=None):
if ctx.message.author.guild_permissions.administrator:
if target is None:
await ctx.send("Please specify a target")
return
if target.lower() == 'role':
await reset_xp_for_role(ctx)
elif target.lower() == 'everyone':
await reset_xp_for_everyone(ctx)
else:
try:
member = await commands.MemberConverter().convert(ctx, target)
await reset_xp_for_member(ctx, member)
except commands.errors.MemberNotFound:
await ctx.send("Member not found.")
else:
await ctx.send("You do not have permission to use this command.")
# Function to reset XP for members with a specific role
async def reset_xp_for_role(ctx):
if ctx.message.author.guild_permissions.administrator:
role_name = "Your Role Name Here" # Change this to the name of the role you want to target
role = discord.utils.get(ctx.guild.roles, name=role_name)
if role:
for member in role.members:
reset_xp(member.id, 0) # Reset XP to 0 for each member
await ctx.send(f"XP reset to 0 for members with the role {role_name}.")
else:
await ctx.send(f"Role '{role_name}' not found.")
# Function to reset XP for everyone in the server
async def reset_xp_for_everyone(ctx):
if ctx.message.author.guild_permissions.administrator:
for member in ctx.guild.members:
if not member.bot: # Ignore bot accounts
reset_xp(member.id, 0) # Reset XP to 0 for each member
await logging.info("XP reset to 0 for everyone in the server.")
# Function to reset XP for a specified member
async def reset_xp_for_member(ctx, member):
reset_xp(member.id, 0) # Reset XP to 0 for the specified member
await ctx.send(f"XP reset to 0 for {member.display_name}.")
# Remove XP
@bot.command(name='RemoveXp')
async def remove_xp_command(ctx, xp: int, member: discord.Member):
"""Remove Xp"""
if ctx.message.author.guild_permissions.administrator:
if member is None:
embed = discord.Embed(title="Oops!", description="Please add a member")
await ctx.send(embed=embed)
if xp is None:
embed = discord.Embed(title="Oops!", description="It's **';addcash <amount> @<user>'**")
await ctx.send(embed=embed)
return
try:
xp_int = int(xp)
except ValueError:
embed = discord.Embed(title="Oops!", description="It's **';addcash <amount> @<user>'**")
await ctx.send(embed=embed)
return
remove_xp(member.id, xp_int)
await ctx.send(f"Added {xp} XP to {member.display_name}.")
# XP leaderboard
@bot.command(name="leaderboard-xp")
async def lb_xp(ctx):
"""XP Leaderboard"""
# Get the leaderboard from the database
leaderboard = get_xp_leaderboard()
# Create an embed to display the leaderboard
embed = discord.Embed(title="XP Leaderboard", color=discord.Color.blue())
# Add top users to the embed
for user_id, xp in leaderboard:
user = await bot.fetch_user(user_id)
embed.add_field(name=f"{user.name}", value=f"{xp} XP", inline=False)
# Send the embed
await ctx.send(embed=embed)
#
#
#
#
#
#
#
#
# economy commands
def is_admin():
async def predicate(ctx):
return ctx.author.guild_permissions.administrator
return commands.check(predicate)
# wallet command
@bot.command(name='bal')
async def wallet(ctx, member: discord.Member = None):
"""Display Cash and Bank Balance"""
if member is None:
member = ctx.author
bank_balance = get_bank_balance(member.id)
cash = get_cash(member.id)
logging.info(f"Member ID: {member.id}, Cash: {cash}, Bank Balance: {bank_balance}")
embed = discord.Embed(title=f"{member.display_name}'s Wallet", color=custom_color)
embed.add_field(name=":four_leaf_clover: Cash", value=f"{cash}", inline=True)
embed.add_field(name=":bank: Bank", value=f"{bank_balance}", inline=True)
await ctx.send(embed=embed)
# add cash command
@bot.command(name='addcash')
@is_admin()
async def addcash(ctx, amount: int = None, member: discord.Member = None):
"""Add Cash (for debugging)"""
if ctx.message.author.guild_permissions.administrator:
if member is None:
member = ctx.author
if amount is None:
await ctx.send("Please add an amount")
return
try:
amount_int = int(amount)
except ValueError:
embed = discord.Embed(title="Oops!", description="It's **';addcash <amount> @<user>'**")
await ctx.send(embed=embed)
return
print(f"Adding {amount} :four_leaf_clover: to Member ID: {member.id}")
add_cash(member.id, amount)
logging.info(f"Added {amount_int} cash to Member ID: {member.id}")
await ctx.send(f"Added {amount}:four_leaf_clover: to {member.display_name}'s account.")
# remove cash command
@bot.command(name="removecash")
@is_admin()
async def removecash(ctx, amount:int = None, member: discord.Member = None) :
"""Remove cash from one member"""
if ctx.message.author.guild_permissions.administrator:
if member is None:
embed = discord.Embed(title="Oops!", description="please mention a member")
await ctx.send(embed = embed)
if amount is None:
embed = discord.Embed(title="Oops!", description="Please add an amount")
await ctx.send(embed=embed)
try:
amount_int = int(amount)
except ValueError:
embed = discord.Embed(title="Oops!", description="It's **';removecash <amount> @<user>'**")
await ctx.send(embed=embed)
return
print(f"Removed {amount} :four_leaf_clover: from {member.display_name}'s account")
remove_cash(member.id, amount)
logging.info(f"removed {amount_int} cash from member {member.id}")
await ctx.send(f"Removed {amount} :four_leaf_clover: from {member.display_name}'s account")
@bot.command(name="tax")
@is_admin()
async def removecash(ctx, amount:int = None, member: discord.Member = None) :
"""Remove cash from one member"""
if ctx.message.author.guild_permissions.administrator:
if member is None:
embed = discord.Embed(title="Oops!", description="please mention a member")
await ctx.send(embed = embed)
if amount is None:
embed = discord.Embed(title="Oops!", description="Please add an amount")
await ctx.send(embed=embed)
try:
amount_int = int(amount)
except ValueError:
embed = discord.Embed(title="Oops!", description="It's **';removecash <amount> @<user>'**")
await ctx.send(embed=embed)
return
print(f"taxed {amount} :four_leaf_clover: from {member.display_name}'s account")
remove_cash(member.id, amount)
logging.info(f"removed {amount_int} cash from member {member.id}")
await ctx.send(f"taxed {amount} :four_leaf_clover: from {member.display_name}'s account")
# Cash leaderboard
@bot.command(name="leaderboard-cash")
async def lb_cash(ctx):
"""XP Leaderboard"""
# Get the leaderboard from the database
leaderboard = get_cash_leaderboard()
# Create an embed to display the leaderboard
embed = discord.Embed(title="Cash Leaderboard", color=custom_color)
# Add top users to the embed
for user_id, cash in leaderboard:
user = await bot.fetch_user(user_id)
embed.add_field(name=f"{user.name}", value=f"{cash} :four_leaf_clover: ", inline=False)
# Send the embed
await ctx.send(embed=embed)
@bot.command(name="deposit")
async def deposit_command(ctx, amount: int = None):
member_id = ctx.author.id
current_cash = get_cash(member_id)
if amount is None or amount > current_cash:
await ctx.send("You don't have enough cash to deposit that amount.")
else:
deposit(member_id, amount)
embed = discord.Embed(title="Transaction Alert!", description=f"Deposited {amount}", color=custom_color)
await ctx.send(embed=embed)
@bot.command(name="withdraw")
async def withdraw_command(ctx, amount: int = None):
member_id = ctx.author.id
bank_balance = get_bank_balance(member_id)
if amount is None or amount > bank_balance:
await ctx.send("You don't have enough funds in the bank to withdraw that amount.")
else:
withdraw(member_id, amount)
embed = discord.Embed(title="Transaction Alert!", description=f"Withdrawed {amount}", color=custom_color)
await ctx.send(embed=embed)
# Define income amounts for roles
# Define the role names that should receive a daily income
daily_role_names = ["Verified 18+"] # Replace with your role names
# Amount of daily income
daily_income_amount = 500
@bot.event
async def on_ready():
print(f'Bot is ready. Logged in as {bot.user}')
@bot.command(name="daily")
async def daily(ctx):
"""Give the user their daily income if they have the required role and it's been 24 hours since the last claim."""
member = ctx.author
if any(role.name in daily_role_names for role in member.roles):
last_claim_time = get_last_claim_times(member.id, 'daily')
now = datetime.now()
if not last_claim_time or (now - date_parser.parse(last_claim_time)).days >= 1:
add_cash(member.id, daily_income_amount)
update_last_claim_times(member.id, 'daily', now.isoformat())
embed = discord.Embed(title="Income Collected!", description=f"You have received your weekly income of :four_leaf_clover: {daily_income_amount}", color=custom_color)
embed.set_author(name=ctx.author.display_name, icon_url=ctx.author.avatar.url)
await ctx.send(embed=embed)
else:
next_claim_time = date_parser.parse(last_claim_time) + timedelta(days=1)
time_left = next_claim_time - now
hours, remainder = divmod(int(time_left.total_seconds()), 3600)
minutes, _ = divmod(remainder, 60)
embed = discord.Embed(title="Income Not Available Yet", description=f"You need to wait {hours} hours and {minutes} minutes before claiming your next weekly income.", color=discord.Color.red())
embed.set_author(name=ctx.author.display_name, icon_url=ctx.author.avatar.url)
await ctx.send(embed=embed)
else:
embed = discord.Embed(title="oops", description="You do not have the required role to receive a daily income. Get verified or boost to recieve role income :D")
await ctx.send(embed = embed)
last_weekly_payout_date = {}
user_balances = {}
weekly_role_income = {
"Shamrock": 5000, # Replace with your role names and income amounts
"₊˚໒ Staff ୭₊˚": 2500,
"Vanity Link" : 2000,
"Bronze Clover": 2000,
}
# Run the bot
@bot.command(name="weekly")
async def weekly(ctx):
"""Give the user their weekly income if they have the required role and it's been 7 days since the last claim."""
member = ctx.author
member_id = member.id
member_roles = [role.name for role in member.roles if role.name in weekly_role_income]
if member_roles:
total_weekly_income = 0
for role_name in member_roles:
total_weekly_income += weekly_role_income[role_name]
# Get the last weekly claim time from the database
last_weekly_claim = get_last_claim_times(member_id, 'weekly')
if last_weekly_claim is None or (datetime.now() - date_parser.parse(last_weekly_claim)).days >= 7:
# If eligible, update last claim time and give weekly income
update_last_claim_times(member_id, 'weekly', datetime.now())
add_cash(member.id, total_weekly_income)
embed = discord.Embed(title="Income Collected!", description=f"You have received your weekly income of :four_leaf_clover: {total_weekly_income}", color=discord.Color.green())
embed.set_author(name=ctx.author.display_name, icon_url=ctx.author.avatar.url)
await ctx.send(embed=embed)
else:
# Calculate time left before next claim
next_claim_time = date_parser.parse(last_weekly_claim) + timedelta(days=7)
time_left = next_claim_time - datetime.now()
hours, remainder = divmod(int(time_left.total_seconds()), 3600)
minutes, _ = divmod(remainder, 60)
embed = discord.Embed(title="Income Not Available Yet", description=f"You need to wait {hours} hours and {minutes} minutes before claiming your next weekly income.", color=discord.Color.red())
embed.set_author(name=ctx.author.display_name, icon_url=ctx.author.avatar.url)
await ctx.send(embed=embed)
else:
await ctx.send("You do not have the required role to receive a weekly income.")
@bot.command(name="collect")
async def collect(ctx):
embed = discord.Embed(title="Wrong command", description="the command is ;daily and ;weekly :/, get it right!!!")
embed.set_author(name=ctx.author.display_name, icon_url=ctx.author.avatar.url)
embed.set_image(url="https://media.discordapp.net/attachments/1247339536900558960/1250617867955736676/8toqxn.jpg?ex=666b9827&is=666a46a7&hm=d23be4dcfd52fa2e37f97f5b3e0a1c27270be3c04f2537bea653f07496b6af77&=&format=webp")
await ctx.send(embed=embed)
# Run the bot
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
# SHOP COMMANDS
@bot.command(name='inv')
async def inventory(ctx):
member = ctx.author
member_id = member.id
inventory = get_inventory(member_id)
if inventory:
embed = discord.Embed(title="Inventory", color=custom_color)
for item_name, quantity in inventory:
embed.add_field(name=item_name, value=f"Quantity: {quantity}", inline=True)
await ctx.send(embed=embed)
else:
await ctx.send("Your inventory is empty.")
async def shop(ctx):
shop_items = get_shop_items()
items_per_page = 10
if not shop_items:
await ctx.send("There are no items available in the shop.")
return
pages = []
for i in range(0, len(shop_items), items_per_page):
embed = discord.Embed(title="Shop Items", color=custom_color)
for item_name, item_price in shop_items[i:i + items_per_page]:
embed.add_field(name=item_name, value=f"Price: {item_price}", inline=False)
pages.append(embed)
if len(pages) == 1:
await ctx.send(embed=pages[0])
return
current_page = 0
async def get_view(page_num):
view = View()
if page_num > 0:
view.add_item(Button(label="Previous", style=discord.ButtonStyle.primary, custom_id="prev"))
if page_num < len(pages) - 1:
view.add_item(Button(label="Next", style=discord.ButtonStyle.primary, custom_id="next"))
return view
async def send_page(page_num):
page_embed = pages[page_num]
view = await get_view(page_num)
message = await ctx.send(embed=page_embed, view=view)
return message
message = await send_page(current_page)
while True:
def check(interaction):
return interaction.message.id == message.id and interaction.user == ctx.author
try:
interaction = await bot.wait_for("interaction", check=check, timeout=60.0)
except asyncio.TimeoutError:
break
if interaction.data['custom_id'] == 'next':
current_page += 1
await interaction.response.edit_message(embed=pages[current_page], view=await get_view(current_page))
elif interaction.data['custom_id'] == 'prev':
current_page -= 1
await interaction.response.edit_message(embed=pages[current_page], view=await get_view(current_page))
@bot.command(name='additem')
@is_admin()
async def add_item(ctx):
await ctx.send("Please provide the name of the item.")
name = await bot.wait_for('message', check=lambda m: m.author == ctx.author and m.channel == ctx.channel)
await ctx.send("Please provide the price of the item.")
price = await bot.wait_for('message', check=lambda m: m.author == ctx.author and m.channel == ctx.channel)
await ctx.send("Is the item consumable? (yes/no)")
consumable = await bot.wait_for('message', check=lambda m: m.author == ctx.author and m.channel == ctx.channel)
await ctx.send("Please mention the role assigned to this item. If none, type 'none'.")
role_assigned = await bot.wait_for('message', check=lambda m: m.author == ctx.author and m.channel == ctx.channel)
try:
add_shop_item(name.content, int(price.content), consumable.content.lower() == 'yes', role_assigned.content)
await ctx.send(f"Item '{name.content}' added to the shop.")
except Exception as e:
await ctx.send(f"An error occurred: {e}")
@bot.command(name='deleteitem')
@is_admin()
async def delete_item(ctx, item_name):
delete_shop_item(item_name)
await ctx.send("Item deleted from the shop.")
@bot.command(name='edititem')
@is_admin()
async def edit_item(ctx, item_id: int, name=None, price=None, consumable=None, role_assigned=None):
edit_shop_item(item_id, name, price, consumable, role_assigned)
await ctx.send("Item details updated.")
@bot.command(name='buy')
async def buy_item(ctx, item_name):
member_id = ctx.author.id
# Check if the item exists in the shop
shop_items = get_shop_items()
item = next((item for item in shop_items if item[0] == item_name), None)
if not item:
await ctx.send("This item does not exist in the shop.")
return
# Check if the member has enough cash to buy the item
cash_balance = get_cash(member_id)
item_price = item[1] # Price of the item
if cash_balance < item_price:
await ctx.send("You don't have enough cash to buy this item.")
return
# Deduct the price of the item from the member's balance
remove_cash(member_id, item_price)
# Add the item to the member's inventory
add_inventory_item(member_id, item_name)
await ctx.send(f"Congratulations! You have bought {item_name} for {item_price} cash.")
async def give_role(ctx, role_name_or_id: str, member: discord.Member):
guild = ctx.guild
try:
# Attempt to retrieve the role by ID first
role = discord.utils.get(guild.roles, id=int(role_name_or_id))
if not role:
# If role ID retrieval fails, attempt to retrieve by name
role = discord.utils.get(guild.roles, name=role_name_or_id)
if role:
# Check if the user already has the role
if role in member.roles:
await ctx.send(f"{member.mention}, you already have the {role.name} role.")
else:
# Attempt to assign the role
await member.add_roles(role)
await ctx.send(f"{member.mention}, you have been given the {role.name} role.")
logging.info(f"{member} was given the {role.name} role manually.")
else:
await ctx.send(f"{ctx.author.mention}, the role '{role_name_or_id}' does not exist.")
logging.warning(f"Role '{role_name_or_id}' not found.")
except ValueError:
await ctx.send(f"{ctx.author.mention}, please provide a valid role name or ID.")
except discord.Forbidden:
await ctx.send(f"{ctx.author.mention}, I do not have permission to manage roles.")
except Exception as e:
await ctx.send(f"{ctx.author.mention}, an unexpected error occurred: {e}")
logging.exception(f"Unexpected error occurred while giving role to {member}: {e}")
@bot.command(name='use')
async def use_item(ctx, member: discord.Member, *, item_name: str):
member_id = ctx.author.id # Get the ID of the command invoker (ctx.author)
guild = ctx.guild
try:
role_identifier = use_inventory_item(member_id, item_name)
if role_identifier:
# Check if role_identifier is a name or ID
try:
role_id = int(role_identifier)
role = discord.utils.get(guild.roles, id=role_id)
except ValueError:
role = discord.utils.get(guild.roles, name=role_identifier)
if role:
# Check if the bot has permission to manage roles for the member
if not ctx.guild.me.guild_permissions.manage_roles:
await ctx.send(f"{ctx.author.mention}, I do not have permission to manage roles.")
return
# Check if the bot's role is higher than the role to be assigned
if role.position >= ctx.guild.me.top_role.position:
await ctx.send(f"{ctx.author.mention}, I cannot assign the role '{role.name}' because it is higher than my highest role.")
return
# Assign the role using the give_role function
await give_role(ctx, str(role.id), member)
logging.info(f"{member} used {item_name} and received the {role.name} role.")
else:
await ctx.send(f"{ctx.author.mention}, you have successfully used {item_name}, but the role '{role_identifier}' does not exist or I cannot assign it.")
logging.warning(f"Role '{role_identifier}' not found or cannot be assigned by {bot.user}.")
else:
await ctx.send(f"{ctx.author.mention}, you have successfully used {item_name}.")
logging.info(f"{ctx.author} used {item_name} without receiving a role.")
except ValueError as e:
await ctx.send(f"{ctx.author.mention}, an error occurred: {e}")
logging.error(f"ValueError occurred while using item '{item_name}': {e}")
except discord.Forbidden:
await ctx.send(f"{ctx.author.mention}, I do not have permission to manage roles.")
logging.error(f"Bot does not have permission to manage roles.")
except Exception as e:
await ctx.send(f"{ctx.author.mention}, an unexpected error occurred: {e}")
logging.exception(f"Unexpected error occurred while using item '{item_name}': {e}")
#
#
#
#
#
#
# gambling commands
# Define card ranks, suits, and values
# Card values
suits = ['♢', '♧', '♡', '♤']
values = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
# Create a full deck of cards
deck = [f'{value}{suit}' for suit in suits for value in values]
# Card values for blackjack
card_values = {
'2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '10': 10,
'J': 10, 'Q': 10, 'K': 10, 'A': 11
}
def deal_card():
return random.choice(deck)
# Calculate hand value
def calculate_hand_value(hand):
value = sum(card_values[card[:-1]] for card in hand)
aces = sum(1 for card in hand if card[:-1] == 'A')
while value > 21 and aces:
value -= 10
aces -= 1
return value
error_color = discord.Color.from_rgb(204, 0, 0)
@bot.command(name='blackjack')
@commands.cooldown(1, 86400, commands.BucketType.user)
async def blackjack(ctx, bet: int):
member_id = ctx.author.id
current_cash = get_cash(member_id)
if bet < 100 or bet > 1000:
embed = discord.Embed(title="Error!", description='Bet must be between 100 and 1000.', color=error_color)
await ctx.send(embed=embed)
# Reset cooldown for this specific user in this specific context
blackjack.reset_cooldown(ctx)
return
if current_cash < bet:
embed = discord.Embed(title="Error!", description=f'You do not have enough cash to place this bet. Your current cash is {current_cash}.', color=error_color)
await ctx.send(embed=embed)
# Reset cooldown for this specific user in this specific context
blackjack.reset_cooldown(ctx)
return
# Deduct the bet from the player's cash
remove_cash(member_id, bet)
player_hand = [deal_card(), deal_card()]
dealer_hand = [deal_card(), deal_card()]
embed = discord.Embed(title="Blackjack", description=f"Bet: {bet}", color=discord.Color.blue())
embed.set_author(name=ctx.author.display_name, icon_url=ctx.author.avatar.url)
embed.add_field(name="Your Hand", value=f'{" ".join(player_hand)} (value: {calculate_hand_value(player_hand)})', inline=False)
embed.add_field(name="Dealer's Hand", value=f'{dealer_hand[0]} ?', inline=False)
message = await ctx.send(embed=embed)
while calculate_hand_value(player_hand) < 21:
await ctx.send('Do you want to hit or stand? (respond with `hit` or `stand`)')
def check(m):
return m.author == ctx.author and m.channel == ctx.channel and m.content.lower() in ['hit', 'stand']
try:
msg = await bot.wait_for('message', check=check, timeout=30.0)
except asyncio.TimeoutError:
await ctx.send('You took too long to respond. Standing by default.')
break
if msg.content.lower() == 'hit':
player_hand.append(deal_card())
embed.set_field_at(0, name="Your Hand", value=f'{" ".join(player_hand)} (value: {calculate_hand_value(player_hand)})', inline=False)
await message.edit(embed=embed)
else:
break
player_value = calculate_hand_value(player_hand)
if player_value > 21:
await ctx.send(f'You busted with a hand value of {player_value}. You lose your bet of {bet}.')
return
embed.set_field_at(1, name="Dealer's Hand", value=f'{" ".join(dealer_hand)} (value: {calculate_hand_value(dealer_hand)})', inline=False)
await message.edit(embed=embed)
while calculate_hand_value(dealer_hand) < 17:
dealer_hand.append(deal_card())
embed.set_field_at(1, name="Dealer's Hand", value=f'{" ".join(dealer_hand)} (value: {calculate_hand_value(dealer_hand)})', inline=False)
await message.edit(embed=embed)
dealer_value = calculate_hand_value(dealer_hand)
if dealer_value > 21:
await ctx.send(f'Dealer busts with a hand value of {dealer_value}. You win {bet * 2}!')
add_cash(member_id, bet * 2)
elif dealer_value > player_value:
await ctx.send(f'Dealer wins with {dealer_value} against your {player_value}. You lose your bet of {bet}.')
elif dealer_value < player_value:
await ctx.send(f'You win with {player_value} against dealer\'s {dealer_value}. You win {bet * 2}!')
add_cash(member_id, bet * 2)
else:
await ctx.send(f'It\'s a tie with both having {player_value}. Your bet of {bet} is returned.')
add_cash(member_id, bet)
@blackjack.error
async def blackjack_error(ctx, error):
if isinstance(error, commands.CommandOnCooldown):
embed = discord.Embed(title="Nuh uh!", description=f"You're going too fast. Try again in {int(error.retry_after // 3600)} hours.", color=error_color)
await ctx.send(embed=embed)
elif isinstance(error, commands.MissingRequiredArgument):
embed = discord.Embed(title="Error!", description='Please enter a valid bet amount between 100 and 1000.', color=error_color)
await ctx.send(embed=embed)
await ctx.send(f"To retry the command, use: `;blackjack <bet_amount>` (e.g., `;blackjack 500`)")
# Reset cooldown for this specific user in this specific context
blackjack.reset_cooldown(ctx)
elif isinstance(error, commands.BadArgument):
embed = discord.Embed(title="Error!", description='Please enter a valid numerical bet amount.', color=error_color)
await ctx.send(embed=embed)
await ctx.send(f"To retry the command, use: `;blackjack <bet_amount>` (e.g., `;blackjack 500`)")
# Reset cooldown for this specific user in this specific context
blackjack.reset_cooldown(ctx)
else:
# Default error handling
embed = discord.Embed(title="Error!", description="Something went wrong.", color=error_color)
await ctx.send(embed=embed)
await ctx.send(f"To retry the command, use: `;blackjack <bet_amount>` (e.g., `;blackjack 500`)")
#
#
#
#
#
#
#
#
#
# ROULETTE COMMAND
class RouletteGame:
def __init__(self):
self.slots = {
'0': 'green',
'00': 'green',
'1': 'red', '2': 'black', '3': 'red', '4': 'black', '5': 'red',
'6': 'black', '7': 'red', '8': 'black', '9': 'red', '10': 'black',
'11': 'black', '12': 'red', '13': 'black', '14': 'red', '15': 'black',
'16': 'red', '17': 'black', '18': 'red', '19': 'red', '20': 'black',
'21': 'red', '22': 'black', '23': 'red', '24': 'black', '25': 'red',
'26': 'black', '27': 'red', '28': 'black', '29': 'black', '30': 'red',
'31': 'black', '32': 'red', '33': 'black', '34': 'red', '35': 'black',
'36': 'red'
}
async def play_roulette(self, ctx, bet_amount: int, space: str):
member_id = ctx.author.id
current_cash = get_cash(member_id) # Replace with your method to retrieve user's cash
if bet_amount <= 100 or bet_amount > current_cash or bet_amount > 1000:
embed = discord.Embed(title="Error!", description='Invalid bet amount.', color=error_color)
embed.set_author(name=ctx.author.display_name, icon_url=ctx.author.avatar.url)
await ctx.send(embed=embed)
return
remove_cash(member_id, bet_amount)
# Determine the multiplier based on the space bet on
if space in ["odd", "even", "black", "red"]:
multiplier = 2
else:
multiplier = 35
result = random.choice(list(self.slots.keys()))
result_prompt = f"The ball landed on: **{self.slots[result]} {result}**!\n\n"
if space == "black":
win = 1 if self.slots[result] == "black" else 0
elif space == "red":
win = 1 if self.slots[result] == "red" else 0
elif space == "even":
result_num = int(result)
win = 1 if (result_num % 2) == 0 else 0
elif space == "odd":
result_num = int(result)
win = 1 if (result_num % 2) != 0 else 0
elif space.isdigit(): # Check if the space is a specific number
win = 1 if space == result else 0
else:
# This should not happen under normal circumstances
print("Unexpected condition.")
if win:
winnings = bet_amount * multiplier
add_cash(member_id, winnings)
result_prompt += f"🎉 **Winner:** 🎉\n{ctx.author.mention} won {str(winnings)}!"
color = discord.Color.green()
else:
result_prompt += "**No Winner :(**"
color = error_color
embed = discord.Embed(title="Roulette Result", description=result_prompt, color=color)
embed.set_author(name=ctx.author.display_name, icon_url=ctx.author.avatar.url)
await ctx.send(embed=embed)
roulette_game = RouletteGame()
@bot.command(name='roulette')
@commands.cooldown(1, 86400, commands.BucketType.user)
async def roulette(ctx, bet_amount: int, space: str):
if space not in ["odd", "even", "black", "red"] and not space.isdigit():
embed = discord.Embed(title="Error!", description="Invalid space. Please bet on 'odd', 'even', 'black', 'red', or a specific number (0-36).", color=error_color)
await ctx.send(embed=embed)
return
await roulette_game.play_roulette(ctx, bet_amount, space)
@roulette.error
async def roulette_error(ctx, error):
traceback.print_exc()
if isinstance(error, commands.CommandOnCooldown):
embed = discord.Embed(title="Nuh uh!", description=f"You're going too fast. Try again in {int(error.retry_after // 3600)} hours.", color=error_color)
await ctx.send(embed=embed)
elif isinstance(error, commands.MissingRequiredArgument):
embed = discord.Embed(title="Error!", description='Please enter a valid bet amount between 100 and 1000, and a color (red or black).', color=error_color)
await ctx.send(embed=embed)
await ctx.send(f"To retry the command, use: `;roulette <bet_amount> <color>` (e.g., `;roulette 400 red`)")
# Reset cooldown for this specific user in this specific context
roulette.reset_cooldown(ctx)
elif isinstance(error, commands.BadArgument):
embed = discord.Embed(title="Error!", description='Please enter a valid numerical bet amount and a valid color (red or black).', color=error_color)
await ctx.send(embed=embed)
await ctx.send(f"To retry the command, use: `;roulette <bet_amount> <color>` (e.g., `;roulette 400 red`)")
# Reset cooldown for this specific user in this specific context
roulette.reset_cooldown(ctx)
else:
# Default error handling
embed = discord.Embed(title="Error!", description="Something went wrong.", color=error_color)
await ctx.send(embed=embed)
await ctx.send(f"To retry the command, use: `;roulette <bet_amount> <color>` (e.g., `;roulette 400 red`)")
#
#
#
#
#
#