forked from Yuuzi261/Tweetcord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
88 lines (66 loc) · 2.84 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
import discord
from discord.ext import commands
from discord import app_commands
from dotenv import load_dotenv
import os
from src.log import setup_logger
from src.db_function.init_db import init_db
from configs.load_configs import configs
log = setup_logger(__name__)
load_dotenv()
bot = commands.Bot(command_prefix=configs['prefix'], intents=discord.Intents.all())
@bot.event
async def on_ready():
await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name=configs['activity_name']))
if not(os.path.isfile(f"{os.getenv('DATA_PATH')}tracked_accounts.db")): init_db()
bot.tree.on_error = on_tree_error
for filename in os.listdir('./cogs'):
if filename.endswith('.py'):
await bot.load_extension(f'cogs.{filename[:-3]}')
log.info(f'{bot.user} is online')
slash = await bot.tree.sync()
log.info(f'synced {len(slash)} slash commands')
@bot.command()
@commands.is_owner()
async def load(ctx, extension):
await bot.load_extension(f'cogs.{extension}')
await ctx.send(f'Loaded {extension} done.')
@bot.command()
@commands.is_owner()
async def unload(ctx, extension):
await bot.unload_extension(f'cogs.{extension}')
await ctx.send(f'Un - Loaded {extension} done.')
@bot.command()
@commands.is_owner()
async def reload(ctx, extension):
await bot.reload_extension(f'cogs.{extension}')
await ctx.send(f'Re - Loaded {extension} done.')
@bot.command()
@commands.is_owner()
async def download_log(ctx : commands.context.Context):
message = await ctx.send(file=discord.File('console.log'))
await message.delete(delay=15)
@bot.command()
@commands.is_owner()
async def download_data(ctx : commands.context.Context):
message = await ctx.send(file=discord.File(f"{os.getenv('DATA_PATH')}tracked_accounts.db"))
await message.delete(delay=15)
@bot.command()
@commands.is_owner()
async def upload_data(ctx : commands.context.Context):
raw = await [attachment for attachment in ctx.message.attachments if attachment.filename[-3:] == '.db'][0].read()
with open(f"{os.getenv('DATA_PATH')}tracked_accounts.db", 'wb') as wbf:
wbf.write(raw)
message = await ctx.send('successfully uploaded data')
await message.delete(delay=5)
@bot.event
async def on_tree_error(itn : discord.Interaction, error : app_commands.AppCommandError):
await itn.response.send_message(error, ephemeral=True)
log.warning(f'an error occurred but was handled by the tree error handler, error message : {error}')
@bot.event
async def on_command_error(ctx : commands.context.Context, error : commands.errors.CommandError):
if isinstance(error, commands.errors.CommandNotFound): return
else: await ctx.send(error)
log.warning(f'an error occurred but was handled by the command error handler, error message : {error}')
if __name__ == '__main__':
bot.run(os.getenv('BOT_TOKEN'))