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

Loading…
Cancel
Save