-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
49 lines (41 loc) · 1.33 KB
/
bot.py
File metadata and controls
49 lines (41 loc) · 1.33 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
import discord, asyncio, json, pathlib, os
from discord.ext import commands
from aiohttp import web
from functions import log, bot, send_message
# global
BASEDIR = pathlib.Path(__file__).parent.resolve()
keypath = os.path.join(BASEDIR, "./keys.json")
with open(keypath, "r") as f:
keys = json.load(f)
@bot.event
async def on_ready():
log(f'Logged in as {bot.user.name}', 2)
print(f'Logged in as {bot.user.name}')
async def handle_message(request):
data = await request.json()
channel_id = data.get('channel_id')
message = data.get('message')
if channel_id and message:
await send_message(channel_id, message)
return web.Response(text="Message sent")
else:
return web.Response(text="Invalid data", status=400)
app = web.Application()
app.router.add_post('/send_message', handle_message)
async def start_http_server():
runner = web.AppRunner(app)
await runner.setup()
site = web.TCPSite(runner, 'localhost', 10080)
await site.start()
async def run_bot(token):
await start_http_server()
try:
await bot.start(token)
except KeyboardInterrupt:
await bot.close()
except Exception as e:
log(f"Bot encountered an error: {e}", 1)
await bot.close()
if __name__ == "__main__":
token = keys["TOKEN"]
asyncio.run(run_bot(token))