Bring reactrestrict into the modern discord era

pull/138/head
bobloy 4 years ago
parent 55656ea672
commit 88ef475339

@ -1,3 +1,4 @@
import logging
from typing import List, Union
import discord
@ -5,6 +6,7 @@ from redbot.core import Config, commands
from redbot.core.bot import Red
from redbot.core.commands import Cog
log = logging.getLogger("red.fox_v3.reactrestrict")
class ReactRestrictCombo:
def __init__(self, message_id, role_id):
@ -131,10 +133,12 @@ class ReactRestrict(Cog):
If no such channel or member can be found.
"""
channel = self.bot.get_channel(channel_id)
if channel is None:
raise LookupError("no channel found.")
try:
member = channel.guild.get_member(user_id)
except AttributeError as e:
raise LookupError("No channel found.") from e
raise LookupError("No member found.") from e
if member is None:
raise LookupError("No member found.")
@ -168,7 +172,7 @@ class ReactRestrict(Cog):
"""
channel = self.bot.get_channel(channel_id)
try:
return await channel.get_message(message_id)
return await channel.fetch_message(message_id)
except discord.NotFound:
pass
except AttributeError: # VoiceChannel object has no attribute 'get_message'
@ -186,9 +190,11 @@ class ReactRestrict(Cog):
:param message_id:
:return:
"""
for channel in ctx.guild.channels:
guild: discord.Guild = ctx.guild
for channel in guild.text_channels:
try:
return await channel.get_message(message_id)
return await channel.fetch_message(message_id)
except discord.NotFound:
pass
except AttributeError: # VoiceChannel object has no attribute 'get_message'
@ -251,34 +257,35 @@ class ReactRestrict(Cog):
await ctx.send("Reaction removed.")
@commands.Cog.listener()
async def on_raw_reaction_add(
self, emoji: discord.PartialEmoji, message_id: int, channel_id: int, user_id: int
):
async def on_raw_reaction_add(self, payload: discord.RawReactionActionEvent):
"""
Event handler for long term reaction watching.
:param discord.PartialReactionEmoji emoji:
:param int message_id:
:param int channel_id:
:param int user_id:
:return:
"""
if emoji.is_custom_emoji():
emoji_id = emoji.id
else:
emoji_id = emoji.name
emoji = payload.emoji
message_id = payload.message_id
channel_id = payload.channel_id
user_id = payload.user_id
# if emoji.is_custom_emoji():
# emoji_id = emoji.id
# else:
# emoji_id = emoji.name
has_reactrestrict, combos = await self.has_reactrestrict_combo(message_id)
if not has_reactrestrict:
log.debug("Message not react restricted")
return
try:
member = self._get_member(channel_id, user_id)
except LookupError:
log.exception("Unable to get member from guild")
return
if member.bot:
log.debug("Won't remove reactions added by bots")
return
if await self.bot.cog_disabled_in_guild(self, member.guild):
@ -287,10 +294,12 @@ class ReactRestrict(Cog):
try:
roles = [self._get_role(member.guild, c.role_id) for c in combos]
except LookupError:
log.exception("Couldn't get approved roles from combos")
return
for apprrole in roles:
if apprrole in member.roles:
log.debug("Has approved role")
return
message = await self._get_message_from_channel(channel_id, message_id)

Loading…
Cancel
Save