Last active
May 29, 2021 05:03
-
-
Save CaptainZidgel/7fd71459f85d7d8108556c426624f514 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
A simple Discord bot to display a user queue with add up / unadd buttons via reactions - a basic task most bots will need. | |
This bot uses a single Cog for the purposes of using member attributes to keep track of data in a typical OOP w/ async fashion. | |
""" | |
import discord | |
from discord.ext import commands | |
TOKEN = open("token.txt", "r").read() | |
prefix = "!" | |
bot = commands.Bot(command_prefix=prefix) | |
QReaction = "🍦" | |
Modrole = "Toppings" | |
class CustomBot(commands.Cog): | |
def __init__(self, bot): | |
self.bot = bot | |
self.queue = [] | |
self.home = None | |
self.ready_up_message = None | |
self.ready_list = None | |
@commands.Cog.listener() | |
async def on_ready(self): | |
print("Ready") | |
self.queue = [] | |
async def update_list(self): | |
await self.ready_list.edit(content="**Queue order**\n" + "\n".join([q.display_name for q in self.queue])) | |
@commands.Cog.listener() | |
async def on_reaction_add(self, reaction, user): | |
if user == self.bot.user: | |
return | |
if reaction.message == self.ready_up_message and reaction.emoji == QReaction: | |
self.queue.append(user) | |
await self.update_list() | |
@commands.Cog.listener() | |
async def on_raw_reaction_remove(self, payload): #on_reaction_remove is cached or whatever | |
if payload.user_id == self.bot.user.id: #ignore bots reaction | |
return | |
user = await self.bot.fetch_user(payload.user_id) #payload is stupid ahaha :D | |
if payload.message_id == self.ready_up_message.id and str(payload.emoji) == QReaction: #if reacting to the right message with the right reaction | |
self.queue.remove(user) #I'm pretty sure this is not the most efficient data type for this but I also don't think efficiency is important here | |
await self.update_list() | |
@commands.command() | |
@commands.has_role(Modrole) | |
async def sethome(self, ctx): | |
self.home = ctx.channel | |
print("{} set home to {}".format(ctx.author, ctx.message.channel)) | |
await ctx.message.delete() | |
@commands.command() | |
@commands.has_role(Modrole) | |
async def startq(self, ctx): | |
self.home = ctx.channel | |
self.queue = [] | |
if self.ready_up_message: | |
await self.ready_up_message.delete() | |
self.ready_up_message = None | |
if self.ready_list: #?sometimes here sometimes not | |
await self.ready_list.delete() | |
self.ready_list = None | |
self.ready_up_message = await self.home.send("Reserve your spot in line now! React with {}:".format(QReaction)) | |
await self.ready_up_message.add_reaction(QReaction) | |
self.ready_list = await self.home.send("**Queue order**") | |
await ctx.message.delete() | |
@commands.command() | |
@commands.has_role(Modrole) | |
async def endq(self, ctx): | |
await self.ready_up_message.delete() | |
await self.ready_list.delete() | |
self.ready_up_message = None | |
self.ready_list = None | |
print("{} ended q".format(ctx.author)) | |
await ctx.message.delete() | |
bot.add_cog(CustomBot(bot)) | |
bot.run(TOKEN) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment