Successfully implemented command module.
This commit is contained in:
parent
37928df153
commit
f743c74892
100
bot.py
100
bot.py
@ -1,69 +1,95 @@
|
|||||||
import discord
|
import discord
|
||||||
import random
|
import random
|
||||||
|
from hangfish import Hangfish
|
||||||
|
from command import Command as cmd
|
||||||
|
|
||||||
aquatic = [
|
AQUATIC_EMOJIS = ( "octopus", "squid", "shrimp", "lobster", "oyster", "crab", "blowfish", "tropical_fish", "fish", "dolphin", "whale", "whale2", "shark", )
|
||||||
"octopus",
|
AQUATIC_ANIMALS = ( "octopus", "squid", "shrimp", "lobster", "oyster", "crab", "blowfish", "tropical_fish", "fish", "dolphin", "whale", "shark", )
|
||||||
"squid",
|
|
||||||
"shrimp",
|
|
||||||
"lobster",
|
|
||||||
"oyster",
|
|
||||||
"crab",
|
|
||||||
"blowfish",
|
|
||||||
"tropical_fish",
|
|
||||||
"fish",
|
|
||||||
"dolphin",
|
|
||||||
"whale",
|
|
||||||
"whale2",
|
|
||||||
"shark",
|
|
||||||
]
|
|
||||||
|
|
||||||
def strToEmoji(string):
|
def strToEmoji(string):
|
||||||
return ":"+string+":"
|
return ":"+string+":"
|
||||||
|
|
||||||
def randomAquatic():
|
def randomAquatic():
|
||||||
i = random.randint(0, len(aquatic)-1)
|
return strToEmoji(AQUATIC_EMOJIS[random.randint(0, len(AQUATIC_EMOJIS)-1)])
|
||||||
return strToEmoji(aquatic[i])
|
|
||||||
|
|
||||||
def aquaticAll():
|
def aquaticAll():
|
||||||
string = ""
|
string = ""
|
||||||
for animal in aquatic:
|
for animal in AQUATIC_EMOJIS:
|
||||||
string += strToEmoji(animal)
|
string += strToEmoji(animal)
|
||||||
return string
|
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):
|
class Bot(discord.Client):
|
||||||
|
|
||||||
|
hangfish_instances = {}
|
||||||
|
|
||||||
async def on_ready(self):
|
async def on_ready(self):
|
||||||
print("We have logged in as {}".format(self.user))
|
print("We have logged in as {}".format(self.user))
|
||||||
pass
|
|
||||||
|
|
||||||
async def on_message(self, message):
|
async def on_message(self, message):
|
||||||
if message.author == self.user:
|
if message.author == self.user:
|
||||||
return
|
return
|
||||||
|
|
||||||
print ("{} sent {}".format(message.author, message.content))
|
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 cmd.call(message.channel, message.content)
|
||||||
await message.channel.send("Hello!")
|
|
||||||
|
|
||||||
if message.content.startswith("$aquaticall"):
|
#
|
||||||
await message.channel.send(aquaticAll())
|
# Define callbacks
|
||||||
|
#
|
||||||
|
|
||||||
elif message.content.startswith("$aquatic"):
|
@staticmethod
|
||||||
await message.channel.send(randomAquatic())
|
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
|
# Ensure token on first line, with no whitespaces at the end/beginning
|
||||||
#
|
#
|
||||||
token_file = open("token.txt", "r")
|
|
||||||
token = token_file.readline()
|
|
||||||
token_file.close()
|
|
||||||
print("\'"+token+"\'");
|
|
||||||
|
|
||||||
bot = Bot()
|
try:
|
||||||
bot.run(token);
|
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")
|
||||||
|
15
command.py
15
command.py
@ -19,14 +19,14 @@ class Command:
|
|||||||
return words
|
return words
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def __getCommand(string):
|
def getCommand(string):
|
||||||
words = Command.__getWords(string);
|
words = Command.__getWords(string);
|
||||||
if words[0].startswith(Command.command_prefix):
|
if words[0].startswith(Command.command_prefix):
|
||||||
return words[0][1:]
|
return words[0][1:]
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def __getArgs(string):
|
def getArgs(string):
|
||||||
words = Command.__getWords(string);
|
words = Command.__getWords(string);
|
||||||
return words[1:]
|
return words[1:]
|
||||||
|
|
||||||
@ -35,9 +35,12 @@ class Command:
|
|||||||
Command.__callbacks[command] = callback
|
Command.__callbacks[command] = callback
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def call(command):
|
async def call(channel, command):
|
||||||
func = Command.__callbacks.get(Command.__getCommand(command), None)
|
func = Command.__callbacks.get(Command.getCommand(command), None)
|
||||||
if func is not None:
|
if func is not None:
|
||||||
return func(*tuple(Command.__getArgs(command)))
|
try:
|
||||||
return None
|
await func(channel, *tuple(Command.getArgs(command)))
|
||||||
|
except TypeError:
|
||||||
|
await channel.send("Incorrect usage.")
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user