-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
60 lines (46 loc) · 1.5 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
import os
import discord
from dictionary import Dictionary
class DnDTranslator(discord.Client):
"""
Bot designed to translate some D&D 5e spells and terms.
"""
def __init__(self):
super().__init__()
self.repository = Dictionary()
async def on_ready(self):
"""
Post login initialization.
"""
print('Logged in as', self.user.name)
async def handle_spell_translation(self, spell_name):
"""
Translate spell name.
"""
aliases = await self.repository.translate_spell_name(spell_name)
if aliases:
return '/'.join(sorted(aliases))
return f"\"{spell_name}\" не найдено :disappointed_relieved:"
async def on_message(self, message):
"""
Message handler and command handlers.
"""
content = message.content
result_embed = None
if content.startswith('!spell'):
_, spell_name = content.split(' ', maxsplit=1)
result_embed = discord.Embed(
title=spell_name.capitalize(),
description=await self.handle_spell_translation(spell_name),
colour=0xDEADBF
)
if result_embed:
await self.send_message(message.channel, embed=result_embed)
def main():
"""
Init bot class and start it.
Takes client key from ENV "DISCORD_CLIENT_KEY".
"""
DnDTranslator().run(os.environ['DISCORD_CLIENT_KEY'])
if __name__ == '__main__':
main()