This repository has been archived by the owner on Nov 9, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.py
81 lines (65 loc) · 2.56 KB
/
bot.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
import sys
import requests
import settings
import nextcord
import message_handler
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from events.base_event import BaseEvent
from events import *
from multiprocessing import Process
# Set to remember if the bot is already running, since on_ready may be called
# more than once on reconnects
this = sys.modules[__name__]
this.running = False
# Scheduler that will be used to manage events
sched = AsyncIOScheduler()
###############################################################################
def main():
# Initialize the client
print("Starting up...")
client = nextcord.Client()
# Define event handlers for the client
# on_ready may be called multiple times in the event of a reconnect,
# hence the running flag
@client.event
async def on_ready():
if this.running:
return
this.running = True
r = requests.get('https://covid19.ddc.moph.go.th/api/Cases/today-cases-all')
ans = r.json()
nc = ans[0]['new_case']
await client.change_presence(activity=nextcord.Activity(type=nextcord.ActivityType.watching, name=f'{nc:,} new Covid cases'))
print("Logged in!", flush=True)
# Load all events
print("Loading events...", flush=True)
n_ev = 0
for ev in BaseEvent.__subclasses__():
event = ev()
sched.add_job(event.run, 'interval', (client,),
minutes=event.interval_minutes)
n_ev += 1
sched.start()
print(f"{n_ev} events loaded", flush=True)
# The message handler for both new message and edits
async def common_handle_message(message):
text = message.content
if text.startswith(settings.COMMAND_PREFIX) and text != settings.COMMAND_PREFIX:
cmd_split = text[len(settings.COMMAND_PREFIX):].split()
try:
await message_handler.handle_command(cmd_split[0].lower(),
cmd_split[1:], message, client)
except:
print("Error while handling message", flush=True)
raise
@client.event
async def on_message(message):
await common_handle_message(message)
@client.event
async def on_message_edit(before, after):
await common_handle_message(after)
# Finally, set the bot running
client.run(settings.BOT_TOKEN)
###############################################################################
if __name__ == "__main__":
main()