-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
105 lines (86 loc) · 3.75 KB
/
main.py
File metadata and controls
105 lines (86 loc) · 3.75 KB
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
from distutils.command import config
import logging
import random
import string
from dotenv import load_dotenv
import discord
from dotenv import load_dotenv
import os
import time
import json
import os
import subprocess
from multidict import upstr
from utils.errors import NotVoiceChannel
from utils.parse_config import Config, ErrorMessages
from motor import motor_asyncio
load_dotenv()
# Setup the bot and cogs
config_dict = json.load(open('./config.json'))
intents = discord.Intents().all()
bot = discord.Bot(help_command=None, intents=intents)
bot.config = Config(**config_dict['bot_setup'])
bot.errors = ErrorMessages(**config_dict['errors'])
client = motor_asyncio.AsyncIOMotorClient(os.getenv('MONGO_URI'))
allowed_guilds_collection = client[bot.config.database_name]['allowed_guilds']
allowed_guilds = []
for filename in os.listdir('./Cogs'):
if filename.endswith('.py'):
bot.load_extension(f'Cogs.{filename[:-3]}')
# Logger
logging.basicConfig(filename=f"./{bot.config.logging['log_file']}", level=logging.INFO,
format='%(asctime)s %(levelname)s %(name)s %(message)s')
logger=logging.getLogger(__name__)
@bot.event
async def on_ready():
print(f"Logged in as\n{bot.user}\n-----------")
cursor = allowed_guilds_collection.find()
for document in await cursor.to_list(length=None):
allowed_guilds.append(document['guild_id'])
await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.playing, name=bot.config.version))
@bot.event
async def on_application_command_error(context, exception) -> None:
if isinstance(exception, NotVoiceChannel):
pass
else:
raise exception
# @bot.event
# async def on_interaction(interaction:discord.Interaction):
# if interaction.guild_id is None:
# return
# if str(interaction.guild_id) not in allowed_guilds:
# return
# else:
# await bot.process_application_commands(interaction)
class Commands:
async def log(message):
await message.channel.send(file=discord.File("./chizuru_bot.log"))
logger.info("----------------------------------------Last -log ends here--------------------------------------------")
async def add(message):
args = message.content.split(" ")[1:]
if len(args) == 2:
access_token = get_random_string()
await allowed_guilds_collection.update_one({'guild_id': args[0]}, {"$set":{'guild_id': args[0], 'user_id': args[1], 'access_token': access_token}}, upsert=True)
allowed_guilds.append(args[0])
await message.channel.send("Guild ID: " + args[0] + "\nUser ID: " + args[1] + "\nAccess Token: " + access_token)
else:
return
async def get(message):
guild_id = message.content.split(" ")[1]
document = await allowed_guilds_collection.find_one({'guild_id': guild_id})
await message.channel.send("Guild ID: " + guild_id + "\nUser ID: " + document['user_id'] + "\nAccess Token: " + document['access_token'])
async def guilds(message):
guild_names = "\n".join([guild.name for guild in bot.guilds]).strip()
await message.channel.send(embed=discord.Embed(description=guild_names).set_footer(text=f"{len(bot.guilds)} guilds"))
@bot.event
async def on_message(message:discord.Message):
async def default(message):
return
if message.author.id in bot.config.bot_owner_discord_ids and message.content.startswith("-"):
await getattr(Commands, message.content.split()[0].replace("-", "").lower(), default)(message)
def get_random_string():
# choose from all lowercase letter
letters = string.ascii_lowercase + string.ascii_uppercase + "0123456789"
result_str = ''.join(random.choice(letters) for i in range(10))
return result_str
bot.run(os.getenv("TOKEN"))