feat: add ability to query poll results

This commit is contained in:
2021-07-30 15:19:21 -04:00
parent f9f93afcd1
commit 2336e159c7
4 changed files with 114 additions and 7 deletions

View File

@@ -1,11 +1,13 @@
import discord
import os
from discord.flags import MessageFlags
import requests
import json
import random
from cogs import Programming
from cogs import Fun
from cogs import Finance
from data import poll_cache
from discord.ext import commands
from dotenv import load_dotenv
from pretty_help import DefaultMenu, PrettyHelp
@@ -20,6 +22,7 @@ PREFIX = "!"
intents = discord.Intents.default()
intents.members = True
intents.reactions = True
bot = commands.Bot(command_prefix=PREFIX, intents=intents)
@bot.event
@@ -33,6 +36,7 @@ async def on_ready():
@bot.event
async def on_member_join(member):
""" WELCOME START """
guild = discord.utils.find(lambda g: g.name == DISCORD_GUILD_NAME, bot.guilds) # TODO: check if find will throw an error
welcome_channel = discord.utils.find(lambda c: c.name == "👋welcome", guild.channels) # TODO: remove welcome channel hardcode
@@ -45,6 +49,7 @@ async def on_member_join(member):
await welcome_channel.send(embed=gif_embed)
await welcome_channel.send(f"<@!{member.id}> **Welcome to {DISCORD_GUILD_NAME}!** :wave:")
""" WELCOME END """
@bot.event
async def on_message(message):
@@ -52,6 +57,8 @@ async def on_message(message):
if message.author == bot.user:
return
# TODO: is channel is a proposal channel check for poll command
# repsond with command prefix
if bot.user.mentioned_in(message) and not message.mention_everyone:
await message.channel.send(f"**Learn more about me by running:** `{PREFIX}help`")
@@ -59,6 +66,38 @@ async def on_message(message):
# allow cogs to handle messages too
await bot.process_commands(message)
@bot.event
async def on_raw_reaction_add(event):
# avoid bot self trigger
if event.user_id == bot.user.id:
return
""" POLL START """
# check if message is in cache and if reaction is valid
if event.message_id in poll_cache and event.emoji.name in Fun.OPTION_EMOJIS:
# if user has not voted yet, add their vote
if event.user_id not in poll_cache[event.message_id][event.emoji.name]:
poll_cache[event.message_id][event.emoji.name].append(event.user_id)
""" POLL END """
print(poll_cache)
@bot.event
async def on_raw_reaction_remove(event):
# avoid bot self trigger
if event.user_id == bot.user.id:
return
""" POLL START """
# check if message is in cache and if reaction is valid
if event.message_id in poll_cache and event.emoji.name in Fun.OPTION_EMOJIS:
# if user has voted, remove their vote
if event.user_id in poll_cache[event.message_id][event.emoji.name]:
poll_cache[event.message_id][event.emoji.name].remove(event.user_id)
""" POLL END """
print(poll_cache)
# override default help command
menu = DefaultMenu('◀️', '▶️', '')
bot.help_command = PrettyHelp(navigation=menu, color=discord.Colour.green())