-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.py
132 lines (102 loc) · 3.6 KB
/
bot.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
import os, sys, math, traceback
sys.path.insert(1, os.path.join(sys.path[0], 'cogs'))
from Utilities import Utilities
from Economy import Economy
from Gambling import Gambling
from Games import Games
from Utils import (discord, json, get_prefix)
from discord.ext import commands
from dotenv import load_dotenv
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(command_prefix=get_prefix, intents=intents)
bot.remove_command('help')
bot_prefix = '.'
bot.add_cog(Utilities(bot))
bot.add_cog(Economy(bot))
bot.add_cog(Gambling(bot))
bot.add_cog(Games(bot))
@bot.event
async def on_message(ctx):
global bot_prefix
bot_prefix = get_prefix(None, ctx)
for x in bot.cogs:
cog = bot.cogs[x]
cog.prefix = bot_prefix
if ctx.author.id == bot.user.id:
return
if bot.user.mentioned_in(ctx):
await ctx.channel.send(f'My prefix is `{get_prefix(None, ctx)}`')
await bot.process_commands(ctx)
@bot.event
async def on_ready():
await bot.change_presence(activity=discord.Game(name='.help'))
print(f'{bot.user.name} is active')
@bot.event
async def on_guild_remove(guild):
with open('prefix.json', 'r') as f:
prefixes = json.load(f)
prefixes.pop(str(guild.id))
with open('prefix.json', 'w') as f:
json.dump(prefixes, f, indent = 4)
# Unknown command
@bot.event
async def on_command_error(ctx, error):
embed = discord.Embed(title='', color=discord.Color.red())
# if command has local error handler, return
if hasattr(ctx.command, 'on_error'):
return
# get the original exception
error = getattr(error, 'original', error)
if isinstance(error, commands.CommandNotFound):
embed.title = 'Invalid Command'
await ctx.send('', embed=embed)
return
if isinstance(error, commands.BotMissingPermissions):
missing = [perm.replace('_', ' ').replace('guild', 'server').title() for perm in error.missing_perms]
if len(missing) > 2:
fmt = '{}, and {}'.format("**, **".join(missing[:-1]), missing[-1])
else:
fmt = ' and '.join(missing)
_message = 'I need the **{}** permission(s) to run this command.'.format(fmt)
embed.title = _message
await ctx.send('', embed=embed)
return
if isinstance(error, commands.DisabledCommand):
embed.title = 'This command has been disabled.'
await ctx.send('', embed=embed)
return
if isinstance(error, commands.CommandOnCooldown):
embed.title = 'This command is on cooldown, please retry in `{}s`.'.format(math.ceil(error.retry_after))
await ctx.send('', embed=embed)
return
if isinstance(error, commands.MissingPermissions):
missing = [perm.replace('_', ' ').replace('guild', 'server').title() for perm in error.missing_perms]
if len(missing) > 2:
fmt = '{}, and {}'.format("**, **".join(missing[:-1]), missing[-1])
else:
fmt = ' and '.join(missing)
_message = 'You need the **{}** permission(s) to use this command.'.format(fmt)
embed.title = _message
await ctx.send('', embed=embed)
return
if isinstance(error, commands.UserInputError):
embed.title = 'Invalid Arguments.'
await ctx.send('', embed=embed)
return
if isinstance(error, commands.NoPrivateMessage):
try:
await ctx.author.send('This command cannot be used in direct messages.')
except discord.Forbidden:
pass
return
if isinstance(error, commands.CheckFailure):
embed.title = 'You do not have permission to use this command.'
await ctx.send('', embed=embed)
return
# ignore all other exception types, but print them to stderr
print('Ignoring exception in command {}:'.format(ctx.command), file=sys.stderr)
traceback.print_exception(type(error), error, error.__traceback__, file=sys.stderr)
bot.run(TOKEN)