Successfully implemented command module.

This commit is contained in:
Sheldon Lee 2020-09-15 11:01:02 +00:00
parent 37928df153
commit f743c74892
2 changed files with 76 additions and 47 deletions

94
bot.py
View File

@ -1,69 +1,95 @@
import discord
import random
from hangfish import Hangfish
from command import Command as cmd
aquatic = [
"octopus",
"squid",
"shrimp",
"lobster",
"oyster",
"crab",
"blowfish",
"tropical_fish",
"fish",
"dolphin",
"whale",
"whale2",
"shark",
]
AQUATIC_EMOJIS = ( "octopus", "squid", "shrimp", "lobster", "oyster", "crab", "blowfish", "tropical_fish", "fish", "dolphin", "whale", "whale2", "shark", )
AQUATIC_ANIMALS = ( "octopus", "squid", "shrimp", "lobster", "oyster", "crab", "blowfish", "tropical_fish", "fish", "dolphin", "whale", "shark", )
def strToEmoji(string):
return ":"+string+":"
def randomAquatic():
i = random.randint(0, len(aquatic)-1)
return strToEmoji(aquatic[i])
return strToEmoji(AQUATIC_EMOJIS[random.randint(0, len(AQUATIC_EMOJIS)-1)])
def aquaticAll():
string = ""
for animal in aquatic:
for animal in AQUATIC_EMOJIS:
string += strToEmoji(animal)
return string
def aquatic(*args):
if len(args) > 0:
if args[0] == "-a": return aquaticAll()
else: return "Unknown Argument: {}".format(args[0])
return randomAquatic()
class Bot(discord.Client):
hangfish_instances = {}
async def on_ready(self):
print("We have logged in as {}".format(self.user))
pass
async def on_message(self, message):
if message.author == self.user:
return
print ("{} sent {}".format(message.author, message.content))
if message.content.startswith("$ping"):
await message.channel.send(
"@{} pong!".format(
message.author.mention
)
)
if message.content.startswith("$hello"):
await message.channel.send("Hello!")
await cmd.call(message.channel, message.content)
if message.content.startswith("$aquaticall"):
await message.channel.send(aquaticAll())
#
# Define callbacks
#
elif message.content.startswith("$aquatic"):
await message.channel.send(randomAquatic())
@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)
#
# Ensure token on first line, with no whitespaces at the end/beginning
#
try:
token_file = open("token.txt", "r")
token = token_file.readline()
token_file.close()
print("\'"+token+"\'");
print("\'"+token+"\'")
bot = Bot()
bot.run(token);
bot.run(token)
except discord.errors.LoginFailure:
print("Invalid token")

View File

@ -19,14 +19,14 @@ class Command:
return words
@staticmethod
def __getCommand(string):
def getCommand(string):
words = Command.__getWords(string);
if words[0].startswith(Command.command_prefix):
return words[0][1:]
return ""
@staticmethod
def __getArgs(string):
def getArgs(string):
words = Command.__getWords(string);
return words[1:]
@ -35,9 +35,12 @@ class Command:
Command.__callbacks[command] = callback
@staticmethod
def call(command):
func = Command.__callbacks.get(Command.__getCommand(command), None)
async def call(channel, command):
func = Command.__callbacks.get(Command.getCommand(command), None)
if func is not None:
return func(*tuple(Command.__getArgs(command)))
return None
try:
await func(channel, *tuple(Command.getArgs(command)))
except TypeError:
await channel.send("Incorrect usage.")