2020-07-02 21:04:11 +08:00
|
|
|
import discord
|
|
|
|
import random
|
2020-09-15 19:01:02 +08:00
|
|
|
from hangfish import Hangfish
|
|
|
|
from command import Command as cmd
|
2020-07-02 21:04:11 +08:00
|
|
|
|
2020-09-15 19:01:02 +08:00
|
|
|
AQUATIC_EMOJIS = ( "octopus", "squid", "shrimp", "lobster", "oyster", "crab", "blowfish", "tropical_fish", "fish", "dolphin", "whale", "whale2", "shark", )
|
2020-09-15 23:00:07 +08:00
|
|
|
AQUATIC_ANIMALS = ( "octopus", "squid", "shrimp", "lobster", "oyster", "crab", "blowfish", "fish", "dolphin", "whale", "shark", )
|
2020-07-02 21:04:11 +08:00
|
|
|
|
|
|
|
def strToEmoji(string):
|
|
|
|
return ":"+string+":"
|
|
|
|
|
|
|
|
def randomAquatic():
|
2020-09-15 19:01:02 +08:00
|
|
|
return strToEmoji(AQUATIC_EMOJIS[random.randint(0, len(AQUATIC_EMOJIS)-1)])
|
2020-07-02 21:04:11 +08:00
|
|
|
|
|
|
|
def aquaticAll():
|
|
|
|
string = ""
|
2020-09-15 19:01:02 +08:00
|
|
|
for animal in AQUATIC_EMOJIS:
|
|
|
|
string += strToEmoji(animal)
|
2020-07-02 21:04:11 +08:00
|
|
|
return string
|
|
|
|
|
2020-09-15 19:01:02 +08:00
|
|
|
def aquatic(*args):
|
|
|
|
if len(args) > 0:
|
|
|
|
if args[0] == "-a": return aquaticAll()
|
|
|
|
else: return "Unknown Argument: {}".format(args[0])
|
|
|
|
return randomAquatic()
|
|
|
|
|
|
|
|
|
2020-07-02 21:04:11 +08:00
|
|
|
class Bot(discord.Client):
|
|
|
|
|
2020-09-15 19:01:02 +08:00
|
|
|
hangfish_instances = {}
|
|
|
|
|
2020-07-02 21:04:11 +08:00
|
|
|
async def on_ready(self):
|
|
|
|
print("We have logged in as {}".format(self.user))
|
|
|
|
|
|
|
|
async def on_message(self, message):
|
|
|
|
if message.author == self.user:
|
2020-09-15 19:01:02 +08:00
|
|
|
return
|
2020-07-02 21:04:11 +08:00
|
|
|
|
|
|
|
print ("{} sent {}".format(message.author, message.content))
|
|
|
|
|
2020-09-15 19:01:02 +08:00
|
|
|
await cmd.call(message.channel, message.content)
|
|
|
|
|
|
|
|
#
|
|
|
|
# Define callbacks
|
|
|
|
#
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def getHangfishInstance(channel):
|
|
|
|
return Bot.hangfish_instances.get(channel.id, None)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
async def createHangfishInstance(channel):
|
|
|
|
word = random.choice(AQUATIC_ANIMALS)
|
|
|
|
|
|
|
|
message = None
|
|
|
|
if Bot.getHangfishInstance(channel) is not None:
|
|
|
|
message = "Hangfish instance already created. Creating new instance"
|
|
|
|
else:
|
|
|
|
message = "Creating Hangfish instance."
|
|
|
|
Bot.hangfish_instances[channel.id] = Hangfish(word)
|
|
|
|
await channel.send(message)
|
|
|
|
await channel.send("```"+Bot.getHangfishInstance(channel).getString()+"```")
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
async def updateHangfishInstance(channel, guess):
|
|
|
|
instance = Bot.getHangfishInstance(channel)
|
|
|
|
if instance is None:
|
|
|
|
await channel.send("Hangfish instance not created.")
|
|
|
|
instance.guess(guess)
|
|
|
|
await channel.send("```"+instance.getString()+"```")
|
|
|
|
if not instance.running:
|
|
|
|
del Bot.hangfish_instances[channel.id]
|
|
|
|
await channel.send("Game over.")
|
|
|
|
|
|
|
|
#
|
|
|
|
# Register callbacks
|
|
|
|
#
|
|
|
|
|
|
|
|
cmd.registerCallback("aquatic", aquatic)
|
|
|
|
cmd.registerCallback("hangfish", Bot.createHangfishInstance)
|
|
|
|
cmd.registerCallback("guess", Bot.updateHangfishInstance)
|
2020-07-02 21:04:11 +08:00
|
|
|
|
2020-09-02 18:20:52 +08:00
|
|
|
#
|
|
|
|
# Ensure token on first line, with no whitespaces at the end/beginning
|
|
|
|
#
|
2020-07-02 21:04:11 +08:00
|
|
|
|
2020-09-15 19:01:02 +08:00
|
|
|
try:
|
|
|
|
token_file = open("token.txt", "r")
|
|
|
|
token = token_file.readline()
|
|
|
|
token_file.close()
|
|
|
|
print("\'"+token+"\'")
|
|
|
|
bot = Bot()
|
|
|
|
bot.run(token)
|
|
|
|
except discord.errors.LoginFailure:
|
|
|
|
print("Invalid token")
|