-
Notifications
You must be signed in to change notification settings - Fork 40
/
bot.py
110 lines (98 loc) · 3.82 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
import discord
import os
import importlib_metadata
import json
from discord.ext import commands
from dotenv import load_dotenv
from src.mention_chatbot import get_client
from src.log import setup_logger
load_dotenv()
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='!', intents = intents)
# init loggger
logger = setup_logger(__name__)
def check_version():
required = [line.strip() for line in open('requirements.txt')]
for package in required:
package_name, package_version = package.split('==')
distribution = importlib_metadata.distribution(package_name)
name, version = distribution.metadata['Name'], distribution.version
if package != f'{name}=={version}':
raise ValueError(f'{name} version {version} is installed but does not match the requirements')
@bot.event
async def on_ready():
bot_status = discord.Status.online
bot_activity = discord.Activity(type=discord.ActivityType.playing, name = "/help")
await bot.change_presence(status = bot_status, activity = bot_activity)
for Filename in os.listdir('./cogs'):
if Filename.endswith('.py'):
await bot.load_extension(f'cogs.{Filename[:-3]}')
client = get_client()
await client.set_chatbot()
logger.info(f'{bot.user} is now running!')
try:
synced = await bot.tree.sync()
logger.info(f"Synced {len(synced)} commands")
except Exception as e:
logger.error(e, exc_info=True)
# Load command
@commands.is_owner()
@bot.command()
async def load(ctx, extension):
await bot.load_extension(f'cogs.{extension}')
await ctx.author.send(f'> **Loaded {extension} done.**')
# Unload command
@commands.is_owner()
@bot.command()
async def unload(ctx, extension):
await bot.unload_extension(f'cogs.{extension}')
await ctx.author.send(f'> **Un-Loaded {extension} done.**')
# Empty discord_bot.log file
@commands.is_owner()
@bot.command()
async def clean(ctx):
open('discord_bot.log', 'w').close()
await ctx.author.send(f'> **Successfully emptied the file!**')
# Get discord_bot.log file
@commands.is_owner()
@bot.command()
async def getlog(ctx):
try:
with open('discord_bot.log', 'rb') as f:
file = discord.File(f)
await ctx.author.send(file=file)
await ctx.author.send("> **Send successfully!**")
except:
await ctx.author.send("> **Send failed!**")
# Upload new Bing cookies and restart the bot
@commands.is_owner()
@bot.command()
async def upload(ctx):
try:
if ctx.message.attachments:
for attachment in ctx.message.attachments:
if "json" in attachment.content_type or "text" in attachment.content_type:
content: bytes = await attachment.read()
if os.path.exists("./cookies.json"):
with open("cookies.json", "w", encoding = "utf-8") as f:
json.dump(json.loads(content), f, indent = 2)
else:
os.environ["BING_COOKIES"] = content.decode('utf-8')
client = get_client()
await client.set_chatbot(json.loads(content))
await ctx.author.send(f'> **Upload new cookies successfully!**')
logger.info("Cookies has been setup successfully")
else:
await ctx.author.send("> **Didn't get any json or txt file.**")
else:
await ctx.author.send("> **Didn't get any file**")
except Exception as e:
await ctx.author.send(f"> **Error:{e}**")
logger.error(e, exc_info=True)
finally:
if not isinstance(ctx.channel, discord.abc.PrivateChannel):
await ctx.message.delete()
if __name__ == '__main__':
check_version()
bot.run(os.getenv("DISCORD_BOT_TOKEN"))