Added experimentation with reactions.

This commit is contained in:
Sheldon Lee 2021-09-25 21:17:31 +00:00
parent 342134fbe2
commit 144155259d

57
bot.py
View File

@ -1,21 +1,40 @@
import discord import discord
import random import random
import asyncio
from hangfish import Hangfish from hangfish import Hangfish
from command import Command as cmd from command import Command as cmd
from queue import Queue
AQUATIC_EMOJIS = ( "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", "fish", "dolphin", "whale", "shark", ) AQUATIC_ANIMALS = ( "octopus", "squid", "shrimp", "lobster", "oyster", "crab", "blowfish", "fish", "dolphin", "whale", "shark", )
AQUATIC_EMOJIS_RAW = (
'\U0001F40B',
'\U0001F419',
'\U0001F41A',
'\U0001F41F',
'\U0001F42C',
'\U0001F420',
'\U0001F421',
'\U0001F433',
'\U0001F980',
'\U0001F988',
'\U0001F990',
'\U0001F991',
'\U0001F99E',
'\U0001F9AA',
'\U0001F9AD'
)
def strToEmoji(string): def strToEmoji(string):
return ":"+string+":" return ":"+string+":"
def randomAquatic(): def randomAquatic():
return strToEmoji(AQUATIC_EMOJIS[random.randint(0, len(AQUATIC_EMOJIS)-1)]) return AQUATIC_EMOJIS_RAW[random.randint(0, len(AQUATIC_EMOJIS)-1)]
def aquaticAll(): def aquaticAll():
string = "" string = ""
for animal in AQUATIC_EMOJIS: for animal in AQUATIC_EMOJIS_RAW:
string += strToEmoji(animal) string += animal
return string return string
# #
@ -25,6 +44,7 @@ def aquaticAll():
class Bot(discord.Client): class Bot(discord.Client):
hangfish_instances = {} hangfish_instances = {}
last_reactions = Queue()
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))
@ -37,6 +57,20 @@ class Bot(discord.Client):
await cmd.call(message.channel, message.content) await cmd.call(message.channel, message.content)
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 # Define callbacks
# #
@ -75,12 +109,22 @@ class Bot(discord.Client):
instance = Bot.getHangfishInstance(channel) instance = Bot.getHangfishInstance(channel)
if instance is None: if instance is None:
await channel.send("Hangfish instance not created.") await channel.send("Hangfish instance not created.")
return
instance.guess(guess) instance.guess(guess)
await channel.send("```"+instance.getString()+"```") await channel.send("```"+instance.getString()+"```")
if not instance.running: if not instance.running:
del Bot.hangfish_instances[channel.id] del Bot.hangfish_instances[channel.id]
await channel.send("Game over.") await channel.send("Game over.")
@staticmethod
async def pong(channel):
await channel.send("pong")
return
@staticmethod
async def setRulesChannel(channel):
await channel.send("<insert message>")
return
# #
# Register callbacks # Register callbacks
# #
@ -88,17 +132,22 @@ class Bot(discord.Client):
cmd.registerCallback("aquatic", Bot.aquatic) cmd.registerCallback("aquatic", Bot.aquatic)
cmd.registerCallback("hangfish", Bot.createHangfishInstance) cmd.registerCallback("hangfish", Bot.createHangfishInstance)
cmd.registerCallback("guess", Bot.updateHangfishInstance) cmd.registerCallback("guess", Bot.updateHangfishInstance)
cmd.registerCallback("ping", Bot.pong)
cmd.registerCallback("ruleschannel", Bot.setRulesChannel)
# #
# Ensure token on first line, with no whitespaces at the end/beginning # Ensure token on first line, with no whitespaces at the end/beginning
# #
intents = discord.Intents.default()
intents.reactions = True
try: try:
token_file = open("token.txt", "r") token_file = open("token.txt", "r")
token = token_file.readline() token = token_file.readline()
token_file.close() token_file.close()
print("\'"+token+"\'") print("\'"+token+"\'")
bot = Bot() bot = Bot(intents=intents)
bot.run(token) bot.run(token)
except discord.errors.LoginFailure: except discord.errors.LoginFailure:
print("Invalid token") print("Invalid token")