-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
193 lines (145 loc) · 6.33 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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import io
import os
import discord
from edge.help_message import EDGE_HELP
from mtg.help_message import MTG_HELP
from mtg.scryfall import get_card
from table_top.calculator import calculate_from_message
from table_top.coin.flip import flip_coin_until, flip_coin
from table_top.help_message import GENERAL_HELP
from table_top.roller import get_roll
from utils.aio.requests import client
from utils.database import db
from utils.discord.arguments import (
Game, Hide, SpellItem, Item, Spell, Dice, Expression, Flips, Face, WithThumb, Card, WCLName
)
from utils.discord.extended_bot import Bot
from utils.discord.logging import usage_logger
from utils.discord.types import Interaction
from wow.data.items import item_starting_letter_groups
from wow.data.spells import spell_starting_letter_groups
from wow.data.spells_and_items import object_starting_letter_groups
from wow.help_message import WOW_HELP
from wow.items import item_look_up
from wow.search import look_up
from wow.spells import spell_look_up
from wow.wcl.characters import search_characters, search_character
bot = Bot(command_prefix="/", intents=discord.Intents.all())
@bot.event
async def on_ready():
await db.connect()
await client.__aenter__()
await item_starting_letter_groups()
await spell_starting_letter_groups()
await object_starting_letter_groups()
await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, name="Slash Commands"))
print("id: ", bot.application_id)
synced = await bot.tree.sync()
print(f"Synced {len(synced)} commands")
print("Game bot initialised")
@bot.slash_command(alias="help")
async def help_function(interaction: Interaction, game: Game):
"""Get help with using the bot for a particular game."""
if game == "wow":
help_msg = WOW_HELP
elif game == "mtg":
help_msg = MTG_HELP
elif game == "sweote":
help_msg = EDGE_HELP
else:
help_msg = GENERAL_HELP
return await interaction.response.send_message(help_msg, ephemeral=True)
@bot.slash_command()
async def search(interaction: Interaction, name: SpellItem, hide: Hide = None):
"""Search from an Item or Spell in Wrath of the Lich King Classic (Fuzzy Matches)"""
tooltip, url, name = await look_up(name)
await interaction.response.send_message(
f"<{url}>",
file=discord.File(io.BytesIO(tooltip), f"{name}.png"),
ephemeral=bool(hide)
)
@bot.slash_command()
async def item(interaction: Interaction, item_name: Item, hide: Hide = None):
"""Search for an item in Wrath of the Lich King Classic (Fuzzy Matches)"""
tooltip, url, name = await item_look_up(item_name)
await interaction.response.send_message(
f"<{url}>",
file=discord.File(io.BytesIO(tooltip), f"{name}.png"),
ephemeral=bool(hide)
)
@bot.slash_command()
async def spell(interaction: Interaction, spell_name: Spell, hide: Hide = None):
"""Search for a spell in Wrath of the Lich King Classic (Fuzzy Matches)"""
tooltip, url, name = await spell_look_up(spell_name)
await interaction.response.send_message(
f"<{url}>",
file=discord.File(io.BytesIO(tooltip), f"{name}.png"),
ephemeral=bool(hide)
)
@bot.slash_command()
async def roll(interaction: Interaction, dice: Dice, hide: Hide = None):
"""Roll any number/size of numerical or SW:EotE dice."""
await interaction.response.send_message(get_roll(dice), ephemeral=bool(hide))
@bot.slash_command()
async def calc(interaction: Interaction, expression: Expression):
"""Run a simple numerical calculation"""
return await interaction.response.send_message(calculate_from_message(expression))
@bot.slash_command()
async def flip(interaction: Interaction, number_of_flips: Flips):
"""Flip a coin x amount of times"""
return await interaction.response.send_message(await flip_coin(number_of_flips))
@bot.slash_command()
async def flip_until(interaction: Interaction, face: Face, with_thumb: WithThumb = None):
"""Keep flipping coins until a result is flipped"""
return await interaction.response.send_message(flip_coin_until(face, with_thumb))
@bot.slash_command()
async def card(interaction: Interaction, name: Card):
"""Search for a Magic the Gathering card (Fuzzy Matches)"""
card_info, card_image = await get_card(name)
return await interaction.response.send_message(
file=discord.File(io.BytesIO(card_image), f"{card_info.get('name', 'default').replace(' ', '_')}.png")
)
@bot.slash_command()
async def wcl(interaction: Interaction, name: WCLName, hide: Hide = None):
"""Search for a user on Classic Warcraft Logs"""
message = await search_character(name)
if message:
await interaction.response.send_message(message, ephemeral=bool(hide))
else:
await interaction.response.send_message(f"\"{name}\" does not match anything on WCLs :(", ephemeral=bool(hide))
@bot.tree.context_menu(name="Warcraft Logs")
@usage_logger
async def wcl_user(interaction: Interaction, user: discord.User):
message = await search_characters(user)
if message:
await interaction.response.send_message(message, ephemeral=True)
else:
await interaction.response.send_message("Discord name does not match anything on WCLs :(", ephemeral=True)
@bot.tree.context_menu(name="Warcraft Logs")
@usage_logger
async def wcl_message(interaction: Interaction, message: discord.Message):
message = await search_characters(message.author)
if message:
await interaction.response.send_message(message, ephemeral=True)
else:
await interaction.response.send_message("Discord name does not match anything on WCLs :(", ephemeral=True)
@bot.command(name="server_breakdown")
@usage_logger
async def server_breakdown(ctx):
if bot.application.owner == ctx.author:
breakdown = tuple((guild.name, len(guild.members)) for guild in bot.guilds)
await ctx.send("\n".join(f"{name}: {size}" for name, size in breakdown))
@bot.command(name="servers")
@usage_logger
async def servers(ctx):
if bot.application.owner == ctx.author:
await ctx.send(f"{len(bot.guilds)}")
@bot.command(name="members")
@usage_logger
async def members(ctx):
if bot.application.owner == ctx.author:
await ctx.send(f"{len(tuple(bot.get_all_members()))}")
def run():
bot.run(os.getenv("game_bot_token"))
if __name__ == "__main__":
run()