discord-aquatic-bot/bot.py

154 lines
4.3 KiB
Python
Raw Normal View History

2020-07-02 21:04:11 +08:00
import discord
import random
2021-09-26 05:17:31 +08:00
import asyncio
from hangfish import Hangfish
from command import Command as cmd
2021-09-26 05:17:31 +08:00
from queue import Queue
2020-07-02 21:04:11 +08:00
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", "fish", "dolphin", "whale", "shark", )
2021-09-26 05:17:31 +08:00
AQUATIC_EMOJIS_RAW = (
'\U0001F40B',
'\U0001F419',
'\U0001F41A',
'\U0001F41F',
'\U0001F42C',
'\U0001F420',
'\U0001F421',
'\U0001F433',
'\U0001F980',
'\U0001F988',
'\U0001F990',
'\U0001F991',
'\U0001F99E',
'\U0001F9AA',
'\U0001F9AD'
)
2020-07-02 21:04:11 +08:00
def strToEmoji(string):
return ":"+string+":"
def randomAquatic():
2021-09-26 05:17:31 +08:00
return AQUATIC_EMOJIS_RAW[random.randint(0, len(AQUATIC_EMOJIS)-1)]
2020-07-02 21:04:11 +08:00
def aquaticAll():
string = ""
2021-09-26 05:17:31 +08:00
for animal in AQUATIC_EMOJIS_RAW:
string += animal
2020-07-02 21:04:11 +08:00
return string
2020-09-25 16:25:56 +08:00
#
# Bot
#
2020-07-02 21:04:11 +08:00
class Bot(discord.Client):
hangfish_instances = {}
2021-09-26 05:17:31 +08:00
last_reactions = Queue()
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:
2021-09-26 05:17:31 +08:00
return
2020-07-02 21:04:11 +08:00
print ("{} sent {}".format(message.author, message.content))
await cmd.call(message.channel, message.content)
2021-09-26 05:17:31 +08:00
async def on_raw_reaction_add(self, payload):
#print("Reaction Add:\n Channel: {}\n Message: {}\n User: {}".format(payload.channel_id, payload.message_id, payload.user_id))
message = self.get_channel(payload.channel_id).get_partial_message(payload.message_id)
reaction = randomAquatic()
self.last_reactions.put(reaction)
await message.add_reaction(reaction)
await asyncio.sleep(4)
await message.remove_reaction(self.last_reactions.get(), self.user)
return
async def on_raw_reaction_remove(self, payload):
#print("Reaction Remove:\n Channel: {}\n Message: {}\n User: {}".format(payload.channel_id, payload.message_id, payload.user_id))
return
#
# Define callbacks
#
2020-09-24 22:00:37 +08:00
@staticmethod
async def aquatic(channel, *args):
# Options
if len(args) > 0:
if args[0] == "-a":
await channel.send(aquaticAll())
else:
await channel.send("Unknown Argument: {}".format(args[0]))
# Default option
else:
await 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.")
2021-09-26 05:17:31 +08:00
return
instance.guess(guess)
await channel.send("```"+instance.getString()+"```")
if not instance.running:
del Bot.hangfish_instances[channel.id]
await channel.send("Game over.")
2021-09-26 05:17:31 +08:00
@staticmethod
async def pong(channel):
await channel.send("pong")
return
@staticmethod
async def setRulesChannel(channel):
await channel.send("<insert message>")
return
#
# Register callbacks
#
2020-09-24 22:00:37 +08:00
cmd.registerCallback("aquatic", Bot.aquatic)
cmd.registerCallback("hangfish", Bot.createHangfishInstance)
cmd.registerCallback("guess", Bot.updateHangfishInstance)
2021-09-26 05:17:31 +08:00
cmd.registerCallback("ping", Bot.pong)
cmd.registerCallback("ruleschannel", Bot.setRulesChannel)
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
2021-09-26 05:17:31 +08:00
intents = discord.Intents.default()
intents.reactions = True
try:
token_file = open("token.txt", "r")
token = token_file.readline()
token_file.close()
print("\'"+token+"\'")
2021-09-26 05:17:31 +08:00
bot = Bot(intents=intents)
bot.run(token)
except discord.errors.LoginFailure:
print("Invalid token")