forked from MasterCash/ant-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscordbot.py
76 lines (66 loc) · 1.91 KB
/
discordbot.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
import os
from threading import Thread
from discord.ext import commands
from dotenv import load_dotenv
from datamanager import DataManager
load_dotenv()
TOKEN = os.getenv("DISCORD_TOKEN")
GUILD = os.getenv("GUILD_NAME")
class dataQuery(commands.Cog):
dataManager: DataManager
def __init__(self, dataManager: DataManager) -> None:
super().__init__()
self.dataManager = dataManager
@commands.command()
async def antName(self, ctx, name: str):
print(f"user requested {name} data")
uid = self.dataManager.getIdFromName(name)
if uid is not None:
data = self.dataManager.getDataFromId(uid)
msg = f"{name} existed but No data found at {uid}"
if data is not None:
msg = f'''****Info****
Name: {name}
id: {uid}
alliance: {data[1]}
power: {data[2]}
coords: {data[3]}, {data[4]}
last checked: {data[5]}
'''
await ctx.send(msg)
else:
await ctx.send(f"unable to find {name}")
pass
@commands.command()
async def antId(self, ctx, id: int):
results = self.dataManager.getDataFromId(id)
if results is not None:
await ctx.send(f"data: {results}")
else:
await ctx.send(f"unable to find {id}")
pass
class DiscordBot(commands.Bot):
stopped: bool = True
def __init__(self, command_prefix, dataManager: DataManager):
super().__init__(command_prefix)
self.add_cog(dataQuery(dataManager))
async def on_ready(self):
print(f"{self.user} has connected")
def begin(self):
self.run(TOKEN)
async def stop(self):
await self.close()
class DiscordRunner:
stopped: bool = True
bot: DiscordBot
def __init__(self, dataManager: DataManager) -> None:
self.bot = DiscordBot("!", dataManager)
def start(self):
self.stopped = False
t = Thread(target=self.run)
t.start()
def run(self):
self.bot.begin()
async def stop(self):
self.stopped = True
await self.bot.stop()