-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
140 lines (100 loc) · 4.97 KB
/
main.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
import os
from dotenv import load_dotenv
from discord.ext import commands
from functions import emojify, add_emoji, does_translation_exist, emojify_phrase
load_dotenv()
TOKEN = os.getenv('TOKEN')
client = commands.Bot(command_prefix='!')
@client.event
async def on_ready():
print(f'{client.user} has connected to Discord!')
@client.event
async def on_message(message):
username = str(message.author).split("#")[0]
channel = str(message.channel.name)
user_message = str(message.content)
print(f'Message {user_message} by {username} on {channel}') # prints message log on terminal
await client.process_commands(message) # need this so the commands don't get overwritten
@client.command()
async def translateWord(ctx, *message):
""" user types '!translateWord' followed by a message, bot then replies with that message translated to emojis
message argument a tuple e.g. if they message "hello there", message = ("hello", "there")
"""
output = emojify(message)[0]
unknown = emojify(message)[1]
await ctx.send(output)
if len(unknown): # list isn't empty, there were unknown words
await ctx.send("Consider adding the unknown words to the dictionary with the !addWord command, in the format "
"name emoji \ne.g. '!addWord fire 🔥'")
@client.command()
async def translatePhrase(ctx, *, message):
""" user types '!translatePhrase' followed by a phrase that cannot be translated exactly word by word,
bot then replies with that phrase translated to emojis
message argument a string
"""
output = emojify_phrase(message)[0]
unknown = emojify_phrase(message)[1]
if unknown:
await ctx.send("Consider adding the unknown phrase to the dictionary with the !addPhrase command, "
"in the format !addPhrase [phrase] emoji \ne.g. '!addPhrase [hello there] 👋'")
else:
await ctx.send(output)
@client.command()
async def addWord(ctx, *message):
""" User can add an emoji and its english translation to the dictionary."""
if len(message) != 2:
await ctx.send("Incorrect format. Please try again in the format: !addWord name emoji \ne.g. '!addWord fire 🔥'")
else:
word = message[0]
emoji = message[1]
if not does_translation_exist(word):
add_emoji(word, emoji)
await ctx.send(word + " AKA " + emoji + " has been added to the dictionary!")
else:
stored_emoji = does_translation_exist(word)
await ctx.send(word + " already exists in the dictionary as " + stored_emoji +
"\nIf you would like to update the translation, try the !updateWord command (e.g. "
"'!updateWord fire 🔥')")
@client.command()
async def addPhrase(ctx, *, message):
""" should be in format !addPhrase [phrase] emoji e.g. '!addPhrase [brooke knowles] 😀'
User can add an emoji and its english translation to the dictionary. """
if '[' and "] " in message:
phrase = message.split('[')[1].split(']')[0]
emoji = message.split('] ')[1].split("\\\\")[0]
if not does_translation_exist(phrase):
add_emoji(phrase, emoji)
await ctx.send(phrase + " AKA " + emoji + " has been added to the dictionary!")
else:
stored_emoji = does_translation_exist(phrase)
await ctx.send(phrase + " already exists in the dictionary as " + stored_emoji +
"\nIf you would like to update the translation, try the !updatePhrase command (e.g. "
"'!updatePhrase [hello there] 👋')")
else:
await ctx.send("Incorrect format. Please try again in the format: !addPhrase [phrase] emoji "
"\ne.g. '!addPhrase [hello there] 👋'")
@client.command()
async def updateWord(ctx, *message):
""" User can update a word's emoji translation. """
if len(message) != 2:
await ctx.send("Incorrect format. Please try again in the format: !updateWord name emoji \ne.g. "
"'!updateWord fire 🔥'")
else:
word = message[0]
emoji = message[1]
add_emoji(word, emoji)
await ctx.send("The translation for " + word + " has been updated to " + emoji + "!")
@client.command()
async def updatePhrase(ctx, *, message):
""" User can update a phrases' emoji translation.
should be in format !updatePhrase [phrase] emoji e.g. '!updatePhrase [brooke knowles] 😀'
"""
if '[' and "] " in message:
phrase = message.split('[')[1].split(']')[0]
emoji = message.split('] ')[1].split("\\\\")[0]
add_emoji(phrase, emoji)
await ctx.send("The translation for " + phrase + " has been updated to " + emoji + "!")
else:
await ctx.send("Incorrect format. Please try again in the format: !updatePhrase [phrase] emoji "
"\ne.g. !updatePhrase hello there 👋'")
client.run(TOKEN)