-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
59 lines (51 loc) · 1.71 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
import discord
from discord.ext import commands
import json
import logging
import os
# sends all bot logs back to the console
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
logging.getLogger().setLevel(logging.INFO)
logging.getLogger().addHandler(handler)
# sets up the bot
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(command_prefix='$', intents=intents)
# set up commands
# set presence to "with Python3.9"
# register all cogs
# logs loaded cogs to the console
@bot.event
async def on_ready():
print(f'{bot.user} is connected to the following guilds:')
for guild in bot.guilds:
print(f'{guild.name}(id: {guild.id})')
await bot.change_presence(activity=discord.Game(name='with Python3.9'))
for filename in os.listdir('./cogs'):
if filename.endswith('.py'):
await bot.load_extension(f'cogs.{filename[:-3]}')
print(f'Loaded {filename[:-3]}')
# logs the use of commands in the console
@bot.event
async def on_command(ctx):
print(f'{ctx.author} used {ctx.command}')
# set up commands
@bot.event
async def on_message(message):
# don't respond to ourselves
if message.author == bot.user:
return
# reloads all the cogs
@bot.command()
async def reload(ctx):
for filename in os.listdir('./cogs'):
if filename.endswith('.py'):
bot.reload_extension(f'cogs.{filename[:-3]}')
print(f'Reloaded {filename[:-3]}')
await ctx.send('Reloaded all cogs')
# read the config file
with open('config.json') as f:
config = json.load(f)
# then runs the bot using the token from the config file
bot.run(config['token'], log_handler = handler)