-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
195 lines (169 loc) · 6.91 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
import discord
from discord.ext import commands, tasks
from utils import serversettings as Settings, queues as Queues, update
import os
from config import config
import asyncio
import json
import sys
import nacl
import subprocess
# Initialize Discord bot with command prefix and intents
bot = commands.Bot(command_prefix=commands.when_mentioned_or(""), intents=discord.Intents.all())
tree = bot.tree
is_windows = os.name == 'nt'
### LOAD COGS ###
async def load_cogs():
cogs = config.get_cogs()
for cog in cogs:
try:
await bot.load_extension(f"cogs.{cog}")
print(f"[+] Successfully loaded {cog}")
except Exception as e:
print(f"[-] An error occurred while loading {cog}: {e}")
### CHECK FOR UPDATES EVERY HOUR ###
async def update_msg():
if len(sys.argv) > 1:
if sys.argv[1] == 'updated':
try:
embed = discord.Embed(title="Update complete!", description="Your bot has been updated to the latest version.", color=discord.Color.blue())
app_info = await bot.application_info()
owner = app_info.owner
await owner.send(embed=embed)
except Exception as e:
print(f"[-] An error occurred while sending the update message: {e}")
@tasks.loop(hours=1)
async def check_for_updates():
updates = update.check_upd(is_windows)
# If updates are available, send a message to the bot owner
if updates:
app_info = await bot.application_info()
owner = app_info.owner
embed = discord.Embed(title="Updates are available", description=updates, color=discord.Color.green())
message = await owner.send(embed=embed)
# add reaction to the message to allow the owner to update the bot
await message.add_reaction('✅')
await message.add_reaction('❌')
try:
reaction, _ = await bot.wait_for('reaction_add', check=lambda reaction, user: user == owner and reaction.message == message)
if reaction.emoji == '✅':
update.update(is_windows)
await bot.close()
sys.exit(0)
elif reaction.emoji == '❌':
await owner.send('[-] Update declined. Bot will not be updated.')
await asyncio.sleep(24*3600.0)
except asyncio.CancelledError:
pass
except asyncio.TimeoutError:
await owner.send('[-] Update declined. Bot will not be updated.')
await asyncio.sleep(24*3600.0)
except Exception as e:
pass
### ON READY EVENT ###
@bot.event
async def on_ready():
# Load cogs
await load_cogs()
await update_msg()
print(f'[+] Booted {bot.user}...')
await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, name="/play <song>"), status=discord.Status.dnd)
# Reset queues and fetch settings for all guilds
queues = {}
settings = Settings.get_settings_all()
# Initialize settings for all guilds
try:
await Settings.check_guilds(bot.guilds)
print('[+] Successfully initialized bot settings')
except Exception as e:
print('[!] Error initializing bot settings: ', e)
# Initialize queues for all guilds
try:
for guild in bot.guilds:
queues[str(guild.id)] = []
with open(config.queues, 'w') as f:
json.dump(queues, f, indent=4)
print('[+] Successfully initialized queues')
except Exception as e:
print('[!] Error initializing queues: ', e)
# if playlists are enabled, initialize playlists for all guilds
playlist_cog = bot.get_cog('Playlist')
if playlist_cog is not None:
print('[+] Playlists cog found, initializing playlists')
try:
playlist_cog.initialize_playlists(bot.guilds)
print('[+] Successfully initialized playlists')
except Exception as e:
print('[!] Error initializing playlists: ', e)
# Sync the tree for all guilds
for guild in bot.guilds:
try:
await tree.sync(guild=guild)
print(f'[+] Successfully synced guild {guild.name}')
except Exception as e:
print(f'[!] Error syncing guild {guild.name}: {e}')
# Print URL for inviting the bot to a server
oauth_url = discord.utils.oauth_url(bot.application_id, permissions=discord.Permissions(permissions=8))
print(f'[+] Invite URL: {oauth_url}')
await check_for_updates.start()
### INITIALIZE GUILD SETTINGS AND QUEUES ON GUILD JOIN ###
@bot.event
async def on_guild_join(guild):
Settings.set_guild_settings(guild.id, Settings.default_settings)
Queues.update_queue(guild.id, [])
print(f'[+] Joined {guild.name} with id {guild.id}')
print(f'[+] Successfully initialized config/serversettings.json for {guild.name}')
print(f'[+] Successfully initialized config/queues.json for {guild.name}')
### HANDLE ON MESSAGE EVENT ###
@bot.event
async def on_message(message):
if message.guild is not None:
prefix = Settings.get_settings(message.guild.id)['prefix']
if message.content.startswith(prefix):
message.content = message.content[len(prefix):]
await bot.process_commands(message)
else:
await bot.process_commands(message)
### HANDLE ON COMMAND ERROR EVENT ###
@bot.event
async def on_command_error(ctx, error):
if isinstance(error, commands.MissingPermissions):
await ctx.send(f":x: You need to have `{', '.join(error.missing_permissions)}` permissions to use this command.", ephemeral=True)
return
if isinstance(error, commands.CommandNotFound):
return
if isinstance(error, commands.CheckFailure):
await ctx.send(":x: You do not have permission to use this command.")
return
if isinstance(error, commands.MissingRequiredArgument):
await ctx.send(":x: Missing required argument.")
return
if isinstance(error, commands.BadArgument):
await ctx.send(":x: Invalid argument.")
return
if isinstance(error, commands.CommandOnCooldown):
await ctx.send(f":x: Command on cooldown. Try again in {error.retry_after:.2f} seconds.")
return
if isinstance(error, commands.CommandInvokeError):
await ctx.send(f":x: An error occurred while executing the command.: {error}")
return
raise error
def main(*args):
# Check for updates
if args:
if args[0] == 'updated':
print("[+] Successfully updated to the latest version!")
else:
if update.check_upd(is_windows):
update.update(is_windows)
sys.exit(0)
# Initialize the bot
try:
config.init()
bot.run(config.bot_token)
except Exception as e:
print(f"[-] An error occurred while running the bot: {e}")
return
if __name__ == '__main__':
args = sys.argv[1:]
main(*args)