-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
107 lines (83 loc) · 3.03 KB
/
main.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
"""Anonimowe disco wyznania"""
import asyncio
# Django
import django
# 3rd-party
import discord
from aiohttp import web
from aiohttp.web import Application
from discord import app_commands
from discord.ext.commands import DefaultHelpCommand
from django.apps import apps
from django.conf import settings
from utils import read_json_file
django.setup()
class CustomHelpCommand(DefaultHelpCommand):
def __init__(self, **options):
super().__init__(**options)
self.no_category = 'General'
config = read_json_file('config.json')
desc = 'AnonimoweDiscoWyznania.'
intents = discord.Intents.default()
intents.message_content = True
activity = discord.Game('Made by NosApki')
client = discord.Client(
intents=intents,
command_prefix=config.get('cmd_prefix', '/'),
activity=activity,
description=desc,
help_command=CustomHelpCommand(),
)
tree = app_commands.CommandTree(client)
discord_py = None
@client.event
async def on_ready():
"""Print that bot logged in as."""
print(f'Logged in as {client.user.name} | ID: {client.user.id}!')
await tree.sync()
class AnonymousModal(discord.ui.Modal, title='AnonimoweDiscoWyznania'):
designation = discord.ui.TextInput(
label='Wyznanie',
)
async def on_submit(self, interaction):
designate = apps.get_model('storage', 'Designate')
designate.objects.create(message=f'{self.designation}')
await getattr(
interaction.response,
'send_message',
)(f'Dziękujemy za wyznanie, zostanie ono rozpatrzone niedługo!', ephemeral=True)
@tree.command(description='Wyślij anonimową wiadomość')
async def anonim(interaction):
await interaction.response.send_modal(AnonymousModal())
async def handle_msg(request):
designation_id = config.get('designation_id')
if f'{designation_id}'.isdigit() and (designation_id := int(designation_id)):
channel = client.get_channel(designation_id)
if not channel:
return web.Response(status=404) # Channel not found
data = await request.json()
if data.get('token') != getattr(settings, 'DISCORD_MSG_TOKEN', []):
return web.Response(status=401) # Not authorized
msg_ids = data.get('msg_ids', [])
designate = apps.get_model('storage', 'Designate')
queryset = designate.objects.filter(
id__in=msg_ids,
)
for item in queryset:
await channel.send(item.message)
queryset.update(approved=True)
return web.Response(status=200)
return web.Response(status=412) # Precondition failed
async def start_bot():
await client.start(config.get('token'))
async def background_tasks(app):
app[discord_py] = asyncio.create_task(start_bot())
yield
app[discord_py].cancel()
await app[discord_py]
if __name__ == '__main__':
app = Application()
app.add_routes([web.post('/handle-msg/', handle_msg)])
discord_py = web.AppKey('discord_py', asyncio.Task[None])
app.cleanup_ctx.append(background_tasks)
web.run_app(app, port=2137)