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 = {
|
|
|
|
|
"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-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-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 17:07:25 -04:00
|
|
|
numQuotes = len(quotes)
|
|
|
|
|
if numQuotes > 0:
|
2022-04-11 15:50:40 -04:00
|
|
|
if(query == ""):
|
2022-04-11 15:48:36 -04:00
|
|
|
num = randrange(len(quotes))
|
|
|
|
|
message = await ctx.guild.get_channel(quotes[num]['channelId']).fetch_message(quotes[num]['messageId'])
|
|
|
|
|
await ctx.send(embed=self._buildQuote(message, num+1))
|
|
|
|
|
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:
|
|
|
|
|
message = await ctx.guild.get_channel(quotes[num-1]['channelId']).fetch_message(quotes[num-1]['messageId'])
|
|
|
|
|
await ctx.send(embed=self._buildQuote(message, num))
|
|
|
|
|
else:
|
|
|
|
|
await ctx.send(f"There are only {numQuotes} quotes")
|
2022-04-12 14:13:59 -04:00
|
|
|
elif(re.search("^@.+$", query) is not None):
|
|
|
|
|
"""Case username"""
|
|
|
|
|
member: discord.Member = ctx.guild.get_member_named(query)
|
|
|
|
|
if member is None:
|
|
|
|
|
ctx.send("User not found.\nWho are you talking about? OwO")
|
|
|
|
|
else:
|
|
|
|
|
filteredQuotes = []
|
|
|
|
|
for quote in quotes:
|
|
|
|
|
if quote["authorId"] == member.id:
|
|
|
|
|
filteredQuotes.append(quote)
|
|
|
|
|
if len(filteredQuotes) == 0:
|
|
|
|
|
ctx.send(f"No quotes by {member.name} found.\nSay something funny~ OwO")
|
|
|
|
|
else:
|
|
|
|
|
num = randrange(len(filteredQuotes))
|
|
|
|
|
globalNum = quotes.index(filteredQuotes[num])
|
|
|
|
|
message = await ctx.guild.get_channel(filteredQuotes[num]['channelId']).fetch_message(filteredQuotes[num]['messageId'])
|
|
|
|
|
await ctx.send(embed=self._buildQuote(message, globalNum+1))
|
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-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-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-11 11:37:05 -04:00
|
|
|
else:
|
|
|
|
|
return
|