Files
juniteevee-cogs/reactquote/reactquote.py

208 lines
8.5 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-11 15:48:36 -04:00
import re
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 = {
2022-04-14 16:47:22 -04:00
"quotes": [],
"reactQuotesSettings": {
"outputChannel": None
}
2022-04-11 11:13:02 -04:00
}
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
2022-04-14 22:40:46 -04:00
async def _manualAddQuote(self, guild, author, message):
formattedMsg = {
"messageText": message,
"authorId": author.id
}
guild_group = self.config.guild(guild)
quotes = await guild_group.quotes()
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-14 22:40:46 -04:00
async def _buildQuote(self, ctx:commands.Context, formattedMessage, num:int):
if formattedMessage["messageId"] is not None:
message = await ctx.guild.get_channel(formattedMessage['channelId']).fetch_message(formattedMessage['messageId'])
quote = f"{message.content}\n[(Jump)]({message.jump_url})"
timestamp = message.created_at
author = message.author
else:
message = formattedMessage["messageText"]
quote = f"{message}\n*Added Manually*"
timestamp = ctx.message.created_at
author = ctx.guild.get_member(formattedMessage["authorId"])
embed = discord.Embed(timestamp=timestamp)
embed.set_author(name=author.display_name, icon_url=author.avatar_url)
embed.add_field(name=f"#{num}", value=quote, inline=False)
return embed
def _oldBuildQuote(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-11 15:48:36 -04:00
@commands.guild_only()
2022-04-10 16:01:49 -04:00
@commands.command()
2022-04-11 15:49:54 -04:00
async def quote(self, ctx: commands.Context, *, query: str = ""):
2022-04-11 17:07:25 -04:00
"""
Recall Quote
'quote' returns random
'quote 3' returns the #3 quote
"""
2022-04-11 11:18:14 -04:00
quotes = await self.config.guild(ctx.guild).quotes()
2022-04-11 17:07:25 -04:00
numQuotes = len(quotes)
if numQuotes > 0:
2022-04-11 15:50:40 -04:00
if(query == ""):
2022-04-14 16:47:22 -04:00
"""case random"""
2022-04-11 15:48:36 -04:00
num = randrange(len(quotes))
2022-04-14 22:40:46 -04:00
embed = await self._buildQuote(ctx, quotes[num], num+1)
await ctx.send(embed=embed)
2022-04-11 15:48:36 -04:00
elif(re.search("^\d+$", query) is not None):
2022-04-12 14:13:59 -04:00
"""case id"""
2022-04-11 15:48:36 -04:00
num = int(query)
2022-04-11 17:07:25 -04:00
if num <= numQuotes:
2022-04-14 22:40:46 -04:00
embed = await self._buildQuote(ctx, quotes[num], num+1)
await ctx.send(embed=embed)
2022-04-11 17:07:25 -04:00
else:
await ctx.send(f"There are only {numQuotes} quotes")
2022-04-12 14:21:24 -04:00
elif(len(ctx.message.mentions) > 0):
2022-04-12 14:13:59 -04:00
"""Case username"""
2022-04-12 14:21:24 -04:00
member: discord.Member = ctx.message.mentions[0]
2022-04-12 14:13:59 -04:00
if member is None:
2022-04-12 14:17:35 -04:00
await ctx.send(f"{query} not found.\nWho are you talking about? OwO")
2022-04-12 14:13:59 -04:00
else:
filteredQuotes = []
for quote in quotes:
if quote["authorId"] == member.id:
filteredQuotes.append(quote)
if len(filteredQuotes) == 0:
2022-04-12 14:17:35 -04:00
await ctx.send(f"No quotes by {member.name} found.\nSay something funny~ OwO")
2022-04-12 14:13:59 -04:00
else:
num = randrange(len(filteredQuotes))
globalNum = quotes.index(filteredQuotes[num])
2022-04-14 22:40:46 -04:00
embed = await self._buildQuote(ctx, filteredQuotes[num], globalNum+1)
await ctx.send(embed=embed)
2022-04-12 14:17:35 -04:00
else:
"""Testing case"""
await ctx.send(f"{query} was not picked up. (This is for testing purposes)")
2022-04-11 11:13:02 -04:00
else:
2022-04-12 14:13:59 -04:00
await ctx.send("No quotes added yet.\nSay something funny~ OwO")
2022-04-10 16:01:49 -04:00
2022-04-14 22:40:46 -04:00
@commands.guild_only()
@commands.command()
async def addquote(self, ctx: commands.Context, *, query: str):
"""Manually add quote. @ the speaker of quote to properly credit them"""
if query is None or len(query) == 0:
await ctx.send("No quote provided. What are you saying? OwO")
else:
quotes = await self.config.guild(ctx.guild).quotes()
numQuotes = len(quotes)
quote = f"{query}\n*Added Manually*)"
timestamp = ctx.message.created_at
embed = discord.Embed(timestamp=timestamp)
if len(ctx.message.mentions) > 0:
member: discord.Member = ctx.message.mentions[0]
self._manualAddQuote(self, ctx.guild, member, quote)
await ctx.send(f"New quote manually added by {ctx.author.display_name} as #{numQuotes}")
guild_group = self.config.guild(ctx.guild)
settings = await guild_group.reactQuotesSettings()
if settings["outputChannel"] is not None:
logChan = ctx.guild.get_channel(settings["outputChannel"])
formattedMsg = {
"messageText": quote,
"authorId": member.id
}
embed = await self._buildQuote(ctx, formattedMsg, numQuotes)
await logChan.send(embed=embed)
2022-04-11 12:23:48 -04:00
@commands.admin_or_permissions(manage_guild=True)
2022-04-11 15:48:36 -04:00
@commands.guild_only()
2022-04-11 12:23:48 -04:00
@commands.command()
2022-04-11 12:27:11 -04:00
async def removequote(self, ctx: commands.Context, quote_num:int):
2022-04-11 12:23:48 -04:00
"""Remove Quote"""
2022-04-11 12:28:55 -04:00
await self._removeQuote(ctx.guild, quote_num-1)
2022-04-11 12:27:11 -04:00
await ctx.send(f"Removed quote #{quote_num}.")
2022-04-14 16:47:22 -04:00
@commands.admin_or_permissions(manage_guild=True)
@commands.guild_only()
@commands.command()
async def setquoteout(self, ctx: commands.Context):
"""Sets Channel to be output for new Quotes"""
guild_group = self.config.guild(ctx.guild)
2022-04-14 16:52:32 -04:00
settings = await guild_group.reactQuotesSettings()
2022-04-14 16:47:22 -04:00
settings["outputChannel"] = ctx.channel.id
2022-04-14 16:52:32 -04:00
await guild_group.reactQuotesSettings.set(settings)
2022-04-14 16:47:22 -04:00
await ctx.send("New Quotes will print here.")
@commands.admin_or_permissions(manage_guild=True)
@commands.guild_only()
@commands.command()
async def allquotes(self, ctx: commands.Context):
"""Print all quotes"""
quotes = await self.config.guild(ctx.guild).quotes()
for index, quote in enumerate(quotes):
2022-04-14 22:40:46 -04:00
embed = await self._buildQuote(ctx, quote, index+1)
await ctx.send(embed=embed)
2022-04-14 16:47:22 -04:00
2022-04-11 15:48:36 -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 12:11:17 -04:00
pos = await self._addQuote(message)
if pos >= 0:
2022-04-11 12:31:57 -04:00
await channel.send(f"New quote added by {user.display_name} as #{pos}\n({message.jump_url})")
2022-04-14 16:47:22 -04:00
guild_group = self.config.guild(message.guild)
settings = await guild_group.reactQuotesSettings()
2022-04-14 16:55:28 -04:00
if settings["outputChannel"] is not None:
2022-04-14 16:59:12 -04:00
logChan = message.guild.get_channel(settings["outputChannel"])
2022-04-14 22:40:46 -04:00
await logChan.send(embed=self._oldBuildQuote(message, pos))
2022-04-11 11:37:05 -04:00
else:
return