This repository has been archived by the owner on Sep 21, 2022. It is now read-only.
/
main.py
212 lines (179 loc) · 6.24 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
import discord
import os
from discord.ext import commands, tasks
import random
import logging
from datetime import datetime
TOKEN = "" # Bot Token
GUILDID = 0 # Guild ID
logging.basicConfig(
filename=f"data/logs/{datetime.now().strftime('%Y-%m-%d-%H-%M-%S')}.log",
format='%(asctime)s:%(levelname)s:%(name)s: %(message)s',
level=logging.INFO)
intents = discord.Intents.all()
intents.members = True
bot = commands.Bot(
command_prefix=commands.when_mentioned_or(">>"), help_command=None, intents=intents
)
print(f"Found {len(next(os.walk('data/cogs'))[2])} cogs")
for filename in os.listdir("./data/cogs"):
if filename.endswith(".py"):
bot.load_extension(f"data.cogs.{filename[:-3]}")
print(f" > Loaded {filename[:-3]} [Success]")
@bot.group(invoke_without_command=True)
async def cogs(ctx):
"""
Return error message if no subcommand is given.
"""
await ctx.send("Please specify a subcommand.", ephimeral=True)
@commands.has_guild_permissions(manage_guild=True)
@cogs.command()
async def unload(ctx, cog: str):
"""Unloads a cog."""
cog = cog.lower()
try:
bot.unload_extension(f"data.cogs.{cog}")
await ctx.send(f"Unloaded {cog}", ephemeral=True)
await update_application_commands()
except Exception as e:
await ctx.send(f"Error: {e}", ephemeral=True)
@commands.has_guild_permissions(manage_guild=True)
@cogs.command()
async def load(ctx, cog: str):
"""Loads a cog."""
cog = cog.lower()
try:
bot.load_extension(f"data.cogs.{cog}")
await ctx.send(f"Loaded {cog}", ephemeral=True)
await update_application_commands()
except Exception as e:
await ctx.send(f"Error: {e}", ephemeral=True)
@commands.has_guild_permissions(manage_guild=True)
@cogs.command()
async def disable(ctx, cog: str):
"""Disables and unloads a cog."""
cog = cog.lower()
if not os.path.exists(f"data/cogs/{cog}.py"):
if os.path.exists(f"data/cogs/{cog}.disabled"):
return await ctx.send(
f"`{cog.capitalize()}` is already disabled.", ephemeral=True
)
else:
return await ctx.send(
f"`{cog.capitalize()}`` is not a cog.", ephemeral=True
)
try:
os.rename(f"data/cogs/{cog}.py", f"data/cogs/{cog}.disabled")
bot.unload_extension(f"data.cogs.{cog}")
await ctx.send(f"Disabled `{cog.capitalize()}`", ephemeral=True)
await update_application_commands()
except Exception as e:
if e.__contains__("Cog not loaded"):
await ctx.send(
f"`{cog.capitalize()}` wasn't loaded, disabled anyway...",
ephemeral=True,
)
await ctx.send(f"Error: `{e}`", ephemeral=True)
@commands.has_guild_permissions(manage_guild=True)
@cogs.command()
async def enable(ctx, cog: str):
"""Enables and loads a cog."""
cog = cog.lower()
if not os.path.exists(f"data/cogs/{cog}.disabled"):
if os.path.exists(f"data/cogs/{cog}.py"):
return await ctx.send(
f"`{cog.capitalize()}`is already enabled.", ephemeral=True
)
else:
return await ctx.send(
f"`{cog.capitalize()}`` is not a cog.", ephemeral=True
)
try:
os.rename(f"data/cogs/{cog}.disabled", f"data/cogs/{cog}.py")
bot.load_extension(f"data.cogs.{cog}")
await ctx.send(f"Enabled `{cog.capitalize()}`", ephemeral=True)
await update_application_commands()
except Exception as e:
print(e)
await ctx.send(f"`{cog.capitalize()}` is already enabled.", ephemeral=True)
@commands.has_guild_permissions(manage_guild=True)
@cogs.command()
async def reload(ctx, cog: str):
"""Reloads a cog."""
cog = cog.lower()
try:
bot.reload_extension(f"data.cogs.{cog}")
await ctx.send(f"Reloaded {cog}", ephemeral=True)
await update_application_commands()
except Exception as e:
await ctx.send(f"Error: {e}", ephemeral=True)
@commands.has_guild_permissions(manage_guild=True)
@cogs.command()
async def list(ctx):
"""Lists enabled and disabled cogs separately"""
enabled_cogs = [
f"`{i[:-3].capitalize()}`"
for i in os.listdir("./data/cogs")
if i.endswith(".py")
]
disabled_cogs = [
f"`{i[:-9].capitalize()}`"
for i in os.listdir("./data/cogs")
if i.endswith(".disabled")
]
message = (
f"Enabled cogs: {', '.join(enabled_cogs)}\n"
if enabled_cogs
else "No enabled cogs.\n"
)
message += (
f"Disabled cogs: {', '.join(disabled_cogs)}"
if disabled_cogs
else "No disabled cogs."
)
await ctx.send(
content=message,
ephemeral=True,
)
@disable.error
@enable.error
@load.error
@unload.error
@reload.error
@list.error
async def no_permission(ctx, error):
if isinstance(error, commands.MissingPermissions):
await ctx.send("You don't have permission to do that.", ephemeral=True)
@tasks.loop(seconds=30)
async def status_task():
await bot.change_presence(
activity=discord.Game(
name=f'{random.choice(["discord.io/FITMostar.", "with fire.", "Minecraft.", "with Kez.", "Death Note.", "FIT lmao.", "Destiny", "CS:GO", "VSCode", "C++", "Depression"])}'
)
)
async def update_application_commands():
"""
Checks for a difference between global application (slash)
commands and the ones used, if there are any, update them.
"""
gac = await bot.http.get_guild_commands(bot.application_id, GUILDID) # Update guild ID
gac = {i["name"]: i["description"] for i in gac}
lac = {i.name: i.help for i in bot.commands}
if gac != lac:
print("Updating Application Commands")
await bot.register_application_commands(commands=None)
await bot.register_application_commands(guild=bot.get_guild(GUILDID)) # Update guild ID
@bot.event
async def on_ready():
print("Done loading cogs!")
print(f"Logged in as {bot.user} (ID: {bot.user.id})")
print("------")
await update_application_commands()
print("Bot is ready!")
status_task.start()
async def main():
await bot.login(TOKEN)
await bot.connect()
bot.loop.run_forever()
loop = bot.loop
loop.run_until_complete(main())