-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.py
143 lines (117 loc) · 4.57 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
133
134
135
136
137
138
139
140
141
142
143
# FeedbackBot by timraay
import asyncio
import discord
from discord.ext import commands
from datetime import datetime
from pathlib import Path
import os
from utils import get_config
intents = discord.Intents.all()
command_prefix = get_config()['bot']['CommandPrefix']
bot = commands.Bot(intents=intents, command_prefix=command_prefix, case_insensitive=True)
bot.remove_command('help')
@bot.check
def is_in_guild(ctx):
return ctx.guild is not None
@bot.group(invoke_without_command=True, aliases=['cog'])
@commands.is_owner()
async def module(ctx):
await ctx.send(f"**Available Operations**\n{ctx.prefix}cog reload [cog]\n{ctx.prefix}cog enable <cog>\n{ctx.prefix}cog disable <cog>\n{ctx.prefix}cog info <cog>")
@module.command(aliases=["load"])
@commands.is_owner()
async def enable(ctx, cog: str):
""" Enable a cog """
cog = cog.lower()
if os.path.exists(Path(f"./cogs/{cog}.py")):
await bot.load_extension(f"cogs.{cog}")
await ctx.send(f"Enabled {cog}")
else:
await ctx.send(f"{cog} doesn't exist")
@module.command(aliases=["unload"])
@commands.is_owner()
async def disable(ctx, cog: str):
""" Disable a cog """
cog = cog.lower()
if os.path.exists(Path(f"./cogs/{cog}.py")):
await bot.unload_extension(f"cogs.{cog}")
await ctx.send(f"Disabled {cog}")
else:
await ctx.send(f"{cog} doesn't exist")
@module.command()
@commands.is_owner()
async def reload(ctx, cog: str = None):
""" Reload cogs """
async def reload_cog(ctx, cog):
""" Reloads a cog """
try:
await bot.reload_extension(f"cogs.{cog}")
await ctx.send(f"Reloaded {cog}")
except Exception as e:
await ctx.send(f"Couldn't reload {cog}, " + str(e))
if not cog:
for cog in os.listdir(Path("./cogs")):
if cog.endswith(".py"):
cog = cog.replace(".py", "")
await reload_cog(ctx, cog)
else:
if os.path.exists(Path(f"./cogs/{cog}.py")):
await reload_cog(ctx, cog)
else:
await ctx.send(f"{cog} doesn't exist")
@module.command(aliases=["list"])
@commands.is_owner()
async def info(ctx, cog: str = None):
""" List cogs' commands and events """
if cog:
# A single cog
cog = cog.lower()
if os.path.exists(Path(f"./cogs/{cog}.py")):
cog = bot.get_cog(cog)
if not cog:
await ctx.send(f"{cog} is not a module")
else:
commands_list = [command.name for command in cog.get_commands()]
events_list = [listener.name for listener in cog.get_listeners()]
if not commands_list: commands = "None"
else: commands = ", ".join(commands_list)
if not events_list: events = "None"
else: events = ", ".join(events_list)
embed=discord.Embed(title=f"Module \"{cog.qualified_name}\"")
embed.add_field(name=f"Commands ({str(len(commands_list))})", value=commands, inline=False)
embed.add_field(name=f"Events ({str(len(events_list))})", value=events, inline=False)
await ctx.send(embed=embed)
else:
await ctx.send(f"{cog} doesn't exist")
else:
# All cogs
embed = discord.Embed(title="All modules")
for cog in os.listdir(Path("./cogs")):
if cog.endswith(".py"):
cog = cog.replace(".py", "")
cog = bot.get_cog(cog)
if cog:
commands_list = cog.get_commands()
events_list = cog.get_listeners()
embed.add_field(name=cog.qualified_name, value=f"{str(len(commands_list))} commands & {str(len(events_list))} events", inline=False)
await ctx.send(embed=embed)
async def setup_hook():
for cog in os.listdir(Path("./cogs")):
if cog.endswith(".py"):
try:
cog = f"cogs.{cog.replace('.py', '')}"
await bot.load_extension(cog)
except Exception as e:
print(f"{cog} can not be loaded:")
raise e
print('Loaded all cogs')
try:
await asyncio.wait_for(bot.tree.sync(), timeout=5)
print('Synced app commands')
except asyncio.TimeoutError:
print("Didn't sync app commands. This was likely last done recently, resulting in rate limits.")
print("\nLaunched " + bot.user.name + " on " + str(datetime.now()))
print("ID: " + str(bot.user.id))
bot.setup_hook = setup_hook
# Run the bot
token = get_config()['bot']['Token']
bot.run(token)