-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.py
134 lines (108 loc) · 5.68 KB
/
controller.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
from discord.ext import commands, tasks
import credentials as cred
import discord
import aiohttp, requests, logging
import mdtools
import cocapi
MY_GUILD = discord.Object(id=cred.guild)
APPLICATION_ID = cred.application_id
TOKEN = cred.token
API_LINK = "http://localhost:8000/api/"
VALID_CHANNELS = cred.channels
# logging.basicConfig(level=logging.INFO)
# logger = logging.getLogger('discord')
# logger.setLevel(logging.DEBUG)
# handler = logging.FileHandler(filename='discord.log', encoding='utf-8', mode='w')
# handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
# logger.addHandler(handler)
class CoCBot(commands.Bot):
def __init__(self, *, intents: discord.Intents, application_id: int):
super().__init__(command_prefix='.', intents=intents, application_id=application_id)
self.initial_extensions = [
'cogs.SlashDice',
'cogs.SlashGeneral',
'cogs.SlashVoice',
]
async def setup_hook(self):
# self.background_task.start()
self.session = aiohttp.ClientSession()
for ext in self.initial_extensions:
await self.load_extension(ext)
# await self.tree.sync(guild=MY_GUILD)
# await self.tree.sync()
async def close(self):
await super().close()
await self.session.close()
async def on_ready(self):
print('Ready!')
print(f'Logged in as {self.user} (ID: {self.user.id})')
print('------')
async def on_message(self, message):
# content.message will only work if the intent is set to true and its enabled in the bot settings
parent_id = message.channel.parent_id if isinstance(message.channel, discord.Thread) else None
# if channel is part of approved group and not a DM (ephemerals are DMs)
if (message.channel.id in VALID_CHANNELS or parent_id in VALID_CHANNELS) and not isinstance(message.channel, discord.DMChannel):
# check if player is in the system, if not add.
player = cocapi.get_or_create_player(json={
"name": "",
"discord_name": message.author.name,
"discord_id": message.author.id })
# check if this particular channel is in the system, if not, add.
channel = cocapi.get_or_create_channel(json={
"name": message.channel.name,
"channel_id": message.channel.id,
"parent_id": parent_id })
# save the message to the db
newmessage = cocapi.create_message(json={
"messagetime": message.created_at.isoformat(),
"discord_id": message.id,
"content": message.content,
"reply_msg_id": message.reference.message_id if message.reference is not None else None,
"player": player.get('id'),
"discordchannel": channel.get('id') })
# ignore the bot
if not message.author.bot:
if message.content == 'tree sync':
await message.channel.send('commands synced.')
await self.tree.sync()
if message.content == "check channel type thread":
await message.channel.send(f'Check Channel Type: {isinstance(message.channel, discord.Thread)}')
if message.content == "trains":
#trains = requests.get(f"{API_LINK}trainfacts").json()
await message.channel.send('Did someone say *trains*?')
# TODO: add message to database
if message.content.startswith('BANANABREAD'):
archived = message.channel.archived_threads()
async for archive in archived:
print(archive)
if message.content == "LOAD ALL":
print("working to load all messages in channel or thread...")
async for message in message.channel.history(limit=200):
# reference = message.reference.message_id if message.reference is not None and not message.is_system else None
# check if player is in the system, if not add.
player = cocapi.get_or_create_player(json={
"name": "",
"discord_name": message.author.name,
"discord_id": message.author.id })
# check if this particular channel is in the system, if not, add.
channel = cocapi.get_or_create_channel(json={
"name": message.channel.name,
"channel_id": message.channel.id,
"parent_id": 0 })
# save the message to the db
newmessage = cocapi.create_message(json={
"messagetime": message.created_at.isoformat(),
"discord_id": message.id,
"content": message.content,
"reply_msg_id": message.reference.message_id if message.reference is not None else None,
"player": player.get('id'),
"discordchannel": channel.get('id') })
async def on_raw_message_edit(self, payload):
content = payload.data.get('content', '')
message = cocapi.message_content_update(discord_id=payload.message_id, content=content)
async def on_raw_message_delete(self, payload):
cocapi.message_delete(discord_id=payload.message_id)
intents = discord.Intents.default()
intents.message_content = True
bot = CoCBot(intents=intents, application_id=APPLICATION_ID)
bot.run(TOKEN)