-
Notifications
You must be signed in to change notification settings - Fork 5
/
discordpy.py
executable file
·62 lines (49 loc) · 2.02 KB
/
discordpy.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
#!/usr/bin/env python3
import os
import time
import discord
from discord import Message
from discord.ext import commands
from discord.ext.commands import Context
from extensions.chatgpt import generate_chat_response
import config
from view.ChannelView import ChannelView
intents = discord.Intents.default()
bot = commands.Bot(command_prefix='/', description='Rory bot, haamc private', intents=intents.all())
@bot.event
async def on_ready():
await bot.tree.sync()
print(f'- Logging in: {bot.user.name} / {bot.user.id} -')
@bot.event
async def on_message(msg: Message):
# Writes logs away in folder logs/servername/channelname_date.log
if config.logging_enabled:
logfile = 'logs/{}/{}_{}.log'.format(msg.guild, msg.channel, time.strftime('%Y-%m-%d'))
os.makedirs(os.path.dirname(logfile), exist_ok=True)
with open(logfile, 'a') as log:
log.write('{} <{}> {}\n'.format(
time.strftime('%Y-%m-%dT%H:%M:%S'),
msg.author,
msg.content.replace('\n', '\n ')
))
if msg.author.bot:
return
if msg.author.get_role(config.role['global_mod']) is not None and bot.user.mention in msg.content.split():
ctx = await bot.get_context(msg)
completion = await generate_chat_response(ctx)
await ctx.reply(completion, mention_author=False)
# Required to process commands
await bot.process_commands(msg)
@bot.event
async def setup_hook():
bot.add_view(ChannelView(bot))
for filename in os.listdir(os.path.dirname(os.path.abspath(__file__)) + '/cogs'):
if filename.endswith('.py'):
await bot.load_extension(f'cogs.{filename[:-3]}')
print(f'Loaded cogs.{filename[:-3]}')
for filename in os.listdir(os.path.dirname(os.path.abspath(__file__)) + '/extensions'):
if filename.endswith('.py'):
await bot.load_extension(f'extensions.{filename[:-3]}')
print(f'Loaded extensions.{filename[:-3]}')
if __name__ == '__main__':
bot.run(config.authkey)