Files
juniteevee-cogs/reactquote/reactquote.py

77 lines
2.9 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):
2022-04-11 11:54:33 -04:00
formattedMsg = {
"channelId": msg.channel.id,
2022-04-11 12:11:17 -04:00
"messageId": msg.id,
"authorId": msg.author.id
2022-04-11 11:54:33 -04:00
}
2022-04-11 11:13:02 -04:00
guild_group = self.config.guild(msg.guild)
2022-04-11 11:42:19 -04:00
quotes = await guild_group.quotes()
2022-04-11 12:11:17 -04:00
if quotes.count(formattedMsg) > 0:
return -1
else:
quotes.append(formattedMsg)
await guild_group.quotes.set(quotes)
return len(quotes)
2022-04-11 12:23:48 -04:00
async def _removeQuote(self, guild: discord.Guild, n:int):
guild_group = self.config.guild(guild)
quotes = await guild_group.quotes()
quotes.pop(n)
await guild_group.quotes.set(quotes)
2022-04-09 13:32:53 -04:00
2022-04-11 11:54:33 -04:00
def _buildQuote(self, message, num:int):
2022-04-11 11:13:02 -04:00
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))
2022-04-11 11:59:44 -04:00
message = await ctx.guild.get_channel(quotes[num]['channelId']).fetch_message(quotes[num]['messageId'])
2022-04-11 11:54:33 -04:00
await ctx.send(embed=self._buildQuote(message, num+1))
2022-04-11 11:13:02 -04:00
else:
await ctx.send("No quotes added yet. Say something funny~ OwO")
2022-04-10 16:01:49 -04:00
2022-04-11 12:23:48 -04:00
@commands.admin_or_permissions(manage_guild=True)
@commands.command()
async def removeQuote(self, ctx: commands.Context, n:int):
"""Remove Quote"""
await ctx.send(f"I Got {n}")
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 12:11:17 -04:00
pos = await self._addQuote(message)
if pos >= 0:
2022-04-11 12:13:23 -04:00
await channel.send(f"New quote added by {user.display_name} #{pos}\n({message.jump_url})")
2022-04-11 11:37:05 -04:00
else:
return