Files
juniteevee-cogs/reactquote/reactquote.py

52 lines
2.0 KiB
Python
Raw Normal View History

2022-04-09 13:32:53 -04:00
from redbot.core import commands
2022-04-11 11:13:02 -04:00
from redbot.core import Config
from random import randrange
2022-04-10 16:01:49 -04:00
import discord
2022-04-11 09:33:54 -04:00
from datetime import datetime
2022-04-09 13:32:53 -04:00
class ReactQuote(commands.Cog):
"""Cog to store quotes by reacting with speech bubble"""
def __init__(self, bot):
self.bot = bot
2022-04-11 11:13:02 -04:00
self.config = Config.get_conf(self, identifier=8368710483)
default_guild = {
"quotes": []
}
self.config.register_guild(**default_guild)
2022-04-10 09:29:34 -04:00
2022-04-11 11:13:02 -04:00
async def _addQuote(self, msg:discord.Message):
guild_group = self.config.guild(msg.guild)
async with guild_group.quotes() as quotes:
quotes.append(msg)
2022-04-11 11:31:38 -04:00
return len(await guild_group.quotes())
2022-04-09 13:32:53 -04:00
2022-04-11 11:13:02 -04:00
def _buildQuote(self, message:discord.Message, num:int):
quote = f"{message.content}\n[(Jump)]({message.jump_url})"
timestamp = message.created_at
2022-04-11 09:54:09 -04:00
embed = discord.Embed(timestamp=timestamp)
2022-04-11 10:19:43 -04:00
embed.set_author(name=message.author.display_name, icon_url=message.author.avatar_url)
2022-04-11 11:13:02 -04:00
embed.add_field(name=f"#{num}", value=quote, inline=False)
2022-04-11 09:33:54 -04:00
return embed
2022-04-10 16:01:49 -04:00
@commands.command()
async def quote(self, ctx: commands.Context):
2022-04-10 23:10:05 -04:00
"""Recall Random Quote"""
2022-04-10 16:01:49 -04:00
# Your code will go here
2022-04-11 11:18:14 -04:00
quotes = await self.config.guild(ctx.guild).quotes()
2022-04-11 11:13:02 -04:00
if quotes and len(quotes) > 0:
num = randrange(len(quotes))
await ctx.send(embed=self._buildQuote(quotes[num], num+1))
else:
await ctx.send("No quotes added yet. Say something funny~ OwO")
2022-04-10 16:01:49 -04:00
2022-04-10 23:04:59 -04:00
@commands.Cog.listener()
2022-04-10 16:01:49 -04:00
async def on_raw_reaction_add(self, payload:discord.RawReactionActionEvent):
"""On React"""
2022-04-10 22:37:15 -04:00
if str(payload.emoji) == '💬':
2022-04-10 22:57:13 -04:00
message: discord.Message = await self.bot.get_channel(payload.channel_id).fetch_message(payload.message_id)
2022-04-10 22:37:15 -04:00
user = payload.member
2022-04-10 22:57:13 -04:00
channel: discord.TextChannel = message.channel
2022-04-11 11:33:36 -04:00
"""num = await self._addQuote(message)"""
2022-04-11 11:24:23 -04:00
await channel.send(f"New quote added by {user.display_name} as #{num+1}\n({message.jump_url})")