-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
101 lines (74 loc) · 3.04 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
import discord
import asyncio
from discord.ext import commands
import os
class Bot(commands.Bot):
def __init__(self):
intents = discord.Intents.default()
super().__init__(command_prefix=commands.when_mentioned_or('$'), intents=intents)
async def on_ready(self):
print('Bot online B)')
bot = Bot()
#load cog
@bot.command()
@commands.has_permissions(administrator=True)
async def load(ctx, string):
string = 'cogs.' + string
try:
await bot.load_extension(string)
print('Loaded extension \"{}\"'.format(string))
await ctx.message.channel.send('Loaded extension \"{}\"'.format(string))
except Exception as e:
exc = '{}: {}'.format(type(e).__name__, e)
print('Failed to load extension \"{}\"\n{}'.format(string, exc))
await ctx.message.channel.send('Failed to load extension \"{}\"'.format(string))
#unload cog
@bot.command()
@commands.has_permissions(administrator=True)
async def unload(ctx, string):
string = 'cogs.' + string
try:
await bot.unload_extension(string)
print('Unloaded extension \"{}\"'.format(string))
await ctx.message.channel.send('Unloaded extension \"{}\"'.format(string))
except Exception as e:
exc = '{}: {}'.format(type(e).__name__, e)
print('Failed to unload extension \"{}\"\n{}'.format(string, exc))
#reload cog
@bot.command()
@commands.has_permissions(administrator=True)
async def reload(ctx, string):
string = 'cogs.' + string
try:
await bot.unload_extension(string)
print('Unloaded extension \"{}\"'.format(string))
except Exception as e:
exc = '{}: {}'.format(type(e).__name__, e)
print('Failed to unload extension \"{}\"\n{}'.format(string, exc))
try:
await bot.load_extension(string)
print('Loaded extension \"{}\"'.format(string))
await ctx.message.channel.send('Reloaded extension \"{}\"'.format(string))
except Exception as e:
exc = '{}: {}'.format(type(e).__name__, e)
print('Failed to load extension \"{}\"\n{}'.format(string, exc))
await ctx.message.channel.send('Failed to load extension \"{}\"'.format(string))
async def load_extensions():
for rootfile in os.listdir("./cogs"):
if rootfile.endswith('.py'):
await bot.load_extension(f"cogs.{rootfile[:-3]}")
print(f"cogs.{rootfile[:-3]}")
else:
for subfile in os.listdir(f"cogs/{rootfile}"):
if subfile.endswith("cog.py"):
await bot.load_extension(f"cogs.{rootfile}.{subfile[:-3]}")
print(f"cogs.{rootfile}.{subfile[:-3]}")
async def main():
await load_extensions()
try:
await bot.start(input('input token: '))
except discord.errors.LoginFailure:
print(f'[Error] Failed to run bot. Check your token')
input("Press enter to close the program... ")
if __name__ == "__main__":
asyncio.run(main())