forked from FuseFairy/DiscordBot-EdgeGPT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
111 lines (99 loc) · 3.95 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
import discord
import os
import src.log
import sys
import pkg_resources
import json
from discord.ext import commands
from cogs.event import set_chatbot
from dotenv import load_dotenv
load_dotenv()
bot = commands.Bot(command_prefix='!', intents = discord.Intents.all())
# init loggger
logger = src.log.setup_logger(__name__)
def check_verion() -> None:
# Read the requirements.txt file and add each line to a list
with open('requirements.txt') as f:
required = f.read().splitlines()
# For each library listed in requirements.txt, check if the corresponding version is installed
for package in required:
# Use the pkg_resources library to get information about the installed version of the library
package_name, package_verion = package.split('==')
installed = pkg_resources.get_distribution(package_name)
# Extract the library name and version number
name, version = installed.project_name, installed.version
# Compare the version number to see if it matches the one in requirements.txt
if package != f'{name}=={version}':
logger.error(f'{name} version {version} is installed but does not match the requirements')
sys.exit()
@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]}')
logger.info(f'{bot.user} is now running!')
print("Bot is Up and Ready!")
try:
synced = await bot.tree.sync()
print(f"Synced {len(synced)} commands")
except Exception as e:
print(e)
# 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 str(attachment)[-4:] == ".txt":
content = await attachment.read()
print(json.loads(content))
with open("cookies.json", "w", encoding = "utf-8") as f:
json.dump(json.loads(content), f, indent = 2)
if not isinstance(ctx.channel, discord.abc.PrivateChannel):
await ctx.message.delete()
await set_chatbot(json.loads(content))
await ctx.author.send(f'> **Upload new cookies successfully!**')
logger.warning("\x1b[31mCookies has been setup successfully\x1b[0m")
else:
await ctx.author.send("> **Didn't get any txt file.**")
else:
await ctx.author.send("> **Didn't get any file.**")
except Exception as e:
await ctx.author.send(f">>> **Error: {e}**")
logger.exception(f"Error while upload cookies: {e}")
if __name__ == '__main__':
check_verion()
bot.run(os.getenv("DISCORD_BOT_TOKEN"))