async def convert(self, ctx, argument): message_id, channel_id = self._get_id_matches(argument) channel = ctx.bot.get_channel( channel_id) if channel_id else ctx.channel if not channel: raise ChannelNotFound(channel_id) return discord.PartialMessage(channel=channel, id=message_id)
def reaction_clear_emoji_hook(self, reaction: discord.Reaction): self.set( message=discord.PartialMessage(channel=reaction.message.channel, id=reaction.message.id), channel=reaction.message.channel, guild=reaction.message.guild, )
async def convert(self, ctx: Context, argument: str) -> discord.PartialMessage: guild_id, message_id, channel_id = self._get_id_matches(ctx, argument) channel = self._resolve_channel(ctx, guild_id, channel_id) if not channel: raise ChannelNotFound(channel_id) return discord.PartialMessage(channel=channel, id=message_id)
def message_hook(ctx: EventContext, message: discord.Message, *_args): ctx.set( message=discord.PartialMessage(channel=message.channel, id=message.id), user=ctx.ensure_member(message.author), channel=message.channel, guild=message.guild, )
async def _do_remind(self): target = self.bot.get_channel(int( self.jump_origin.parts[3])) or self.user if self.user is None: return await self.bot.pool.execute( "DELETE FROM reminders WHERE id=$1", self.rm_id) if self.bot.user_cache[self.user.id].get("dm_reminders", False) is True: target = self.user original_reference = discord.PartialMessage( channel=target, id=int(self.jump_origin.parts[-1])) try: await target.send( self.content, allowed_mentions=discord.AllowedMentions(users=[self.user]), reference=original_reference.to_reference(), ) finally: await self.bot.pool.execute( "DELETE FROM reminders WHERE id=$1 AND user_id=$2", self.rm_id, self.user.id, )
async def on_reminder(self, user_id: int, channel_id: int, message_id: int, what: str) -> None: try: user = self.bot.get_user(user_id) or await self.bot.fetch_user( user_id) except discord.NotFound: return channel = self.bot.get_channel(channel_id) guild_id = getattr(getattr(channel, "guild", None), "id", "@me") jump_url = f"https://discord.com/channels/{guild_id}/{channel.id}/{message_id}" embed = discord.Embed(colour=discord.Colour.blurple(), description=what) embed.set_author(name=f"Reminder for {user}.", icon_url=user.avatar.url) if channel is not None: reference = discord.PartialMessage(channel=channel, id=message_id) await channel.send(embed=embed, reference=reference, mention_author=True) return embed.add_field(name=ZWSP, value=f"[Jump!]({jump_url})") await user.send(user.mention, embed=embed, mention_author=True)
def message_hook(self, message: discord.Message, *_args): self.set( message=discord.PartialMessage(channel=message.channel, id=message.id), user=self._ensure_member(message.author), channel=message.channel, guild=message.guild, )
def reaction_clear_hook(self, message: discord.Message, _reaction: discord.Reaction): self.set( message=discord.PartialMessage(channel=message.channel, id=message.id), channel=message.channel, guild=message.guild, )
def reaction_clear_hook(ctx: EventContext, message: discord.Message, _reaction: discord.Reaction): ctx.set( message=discord.PartialMessage(channel=message.channel, id=message.id), emoji=_reaction.emoji, channel=message.channel, guild=message.guild, )
async def convert(self, ctx: Context[BotT], argument: str) -> discord.PartialMessage: guild_id, message_id, channel_id = self._get_id_matches(ctx, argument) channel = self._resolve_channel(ctx, guild_id, channel_id) if not channel or not isinstance(channel, discord.abc.Messageable): raise ChannelNotFound( channel_id) # type: ignore # channel_id won't be None here return discord.PartialMessage(channel=channel, id=message_id)
def reaction_add_hook(self, reaction: discord.Reaction, user: discord.User): self.set( message=discord.PartialMessage(channel=reaction.message.channel, id=reaction.message.id), user=self._ensure_member(user), channel=reaction.message.channel, guild=reaction.message.guild, )
def raw_reaction_clear_hook(self, payload: discord.RawReactionClearEvent): channel = self.bot.get_channel(payload.channel_id) self.set( message=discord.PartialMessage(channel=channel, id=payload.message_id), channel=channel, guild=self.bot.get_guild(payload.guild_id) if payload.guild_id else None, )
def command_hook(self, cmd_ctx: commands.Context): self.set( message=discord.PartialMessage(channel=cmd_ctx.channel, id=cmd_ctx.message.id), user=cmd_ctx.author, channel=cmd_ctx.channel, guild=cmd_ctx.guild, cmd_ctx=cmd_ctx, )
def raw_reaction_clear_hook(ctx: EventContext, payload: discord.RawReactionClearEvent): channel = ctx.client.get_channel(payload.channel_id) ctx.set( message=discord.PartialMessage(channel=channel, id=payload.message_id), channel=channel, guild=ctx.client.get_guild(payload.guild_id) if payload.guild_id else None, )
def reaction_hook(ctx: EventContext, reaction: discord.Reaction, user: discord.User): ctx.set( message=discord.PartialMessage(channel=reaction.message.channel, id=reaction.message.id), emoji=reaction.emoji, user=ctx.ensure_member(user), channel=reaction.message.channel, guild=reaction.message.guild, )
def set_cmd_ctx(self, cmd_ctx: commands.Context): """Set the command context for the event context.""" _ctx_cmd.set(cmd_ctx) self.set( message=discord.PartialMessage(channel=cmd_ctx.channel, id=cmd_ctx.message.id), user=cmd_ctx.author, channel=cmd_ctx.channel, guild=cmd_ctx.guild, ) return self
def raw_reaction_add_hook(self, payload: discord.RawReactionActionEvent): channel = self.bot.get_channel(payload.channel_id) self.set( message=discord.PartialMessage(channel=channel, id=payload.message_id), user=self._ensure_member(payload.user_id, guild_id=payload.guild_id), channel=channel, guild=self.bot.get_guild(payload.guild_id) if payload.guild_id else None, )
def raw_reaction_hook(ctx: EventContext, payload: discord.RawReactionActionEvent): channel = ctx.client.get_channel(payload.channel_id) guild = ctx.client.get_guild( payload.guild_id) if payload.guild_id else None ctx.set( message=discord.PartialMessage(channel=channel, id=payload.message_id), emoji=payload.emoji, user=ctx.ensure_member(payload.user_id, guild=guild), channel=channel, guild=guild, )
async def giveaway_task(bot, end_time, channel_id, message_id): now = datetime.datetime.utcnow().replace(tzinfo=pytz.UTC) seconds_until = (end_time - now).total_seconds() await asyncio.sleep(seconds_until) try: await bot.pool.execute( "DELETE FROM giveaways WHERE message_id = $1", message_id) channel = await bot.fetch_channel(channel_id) message = discord.PartialMessage(channel=channel, id=message_id) bot.dispatch("giveaway_end", message) except asyncpg.PostgresError: pass
async def on_raw_message_edit(self, payload): msg_id = payload.message_id if data := self.messages.get(str(msg_id)): guild = self.bot.get_guild(payload.guild_id) p = discord.PartialMessage(id=msg_id, channel=guild.get_channel( payload.channel_id)) msg = await self.scan(p, content=payload.data["content"]) if msg: channel = guild.get_channel(data["origin"]) await channel.send( f"Detected changes to <https://discord.com/channels/{payload.guild_id}/{payload.channel_id}/{msg_id}>.\n\n{msg}", allowed_mentions=discord.AllowedMentions.none())
def raw_message_hook(ctx: EventContext, payload: discord.RawMessageDeleteEvent): if payload.cached_message: channel = payload.cached_message.channel guild = payload.cached_message.guild user = ctx.ensure_member(payload.cached_message.author, guild=guild) else: channel = ctx.client.get_channel(payload.channel_id) user = None guild = getattr(channel, "guild", None) ctx.set(message=discord.PartialMessage(channel=channel, id=payload.message_id), user=user, channel=channel, guild=guild)
def raw_message_delete_hook(self, payload: discord.RawMessageDeleteEvent): if payload.cached_message: channel = payload.cached_message.channel guild = payload.cached_message.guild user = self._ensure_member(payload.cached_message.author, guild=guild) else: channel = self.bot.get_channel(payload.channel_id) user = None guild = getattr(channel, "guild", None) self.set(message=discord.PartialMessage(channel=channel, id=payload.message_id), user=user, channel=channel, guild=guild)
async def check_if_has_role(self, payload: discord.RawReactionActionEvent): if payload.member.bot: return try: role_id = self.bot.giveaway_roles[payload.message_id] except KeyError: return if role_id not in payload.member._roles: channel = await self.bot.fetch_channel(payload.channel_id) message = await (discord.PartialMessage( channel=channel, id=payload.message_id)).fetch() reaction = message.reactions[0] await reaction.remove(payload.member)
await pending.delete(gctx) # generate embed based on action action = await gamelogutils.action_from_roll_request( gctx, caster, roll_request) if action is not None: embed = gamelogutils.embed_for_action(gctx, action, caster, attack_roll, damage_roll) else: embed = gamelogutils.embed_for_basic_attack( gctx, roll_request.action, caster, attack_roll, damage_roll) # either update the old message or post a new one if pending is not None: partial = discord.PartialMessage(channel=await gctx.destination_channel(), id=pending.message_id) try: await partial.edit(embed=embed) except discord.NotFound: # original message was deleted await gctx.send(embed=embed) else: await gctx.send(embed=embed) # ==== game log send methods ==== # to access, get the cog from the handler function that is making the checks and call these async def _send_preflight(self, ctx, character): """ Call before any dice event processing. Returns a tuple (campaign_id, ddb_user), the latter will be None if the preflight checks fail. """
def get_summary_msg(self): """Gets the Message object of the combat summary.""" return discord.PartialMessage(channel=self.get_channel(), id=self.summary)
async def add_CS_Emojis(ctx): chan = ctx.channel msg = discord.PartialMessage(id=CSMSG, channel=chan) for emoji in CS_Emoji: await msg.add_reaction(emoji)
async def add_welcome_Emojis(ctx): chan = ctx.channel msg = discord.PartialMessage(id=WELCOME, channel=chan) await msg.add_reaction('✅')
update_config(CONFIG, override_file) return CONFIG # Add constructors yaml.FullLoader.add_constructor("!Config", Config.from_yaml) yaml.FullLoader.add_constructor("!ENV", env_var_constructor) # Add discord specific constructors DISCORD_CONSTRUCTORS: dict[str, Callable[..., Any]] = { "Emoji": lambda e: _bot.get_emoji(e), "Guild": lambda g: _bot.get_guild(g), "User": lambda u: _bot.get_user(u), "Channel": lambda g, c: _bot.get_guild(g).get_channel(c), "Member": lambda g, m: _bot.get_guild(g).get_member(m), "Role": lambda g, r: _bot.get_guild(g).get_role(r), "Message": lambda g, c, m: discord.PartialMessage( channel=_bot.get_guild(g).get_channel(c), id=m), } for key, func in DISCORD_CONSTRUCTORS.items(): yaml.FullLoader.add_constructor(f"!{key}", generate_constructor(func))
def add(self, msg: discord.Message) -> None: self.messages.add(discord.PartialMessage(channel=msg.channel, id=msg.id)) # type: ignore
def partial_message(channel: Union[discord.abc.Snowflake], id: int) -> discord.PartialMessage: return discord.PartialMessage(channel=channel, id=id) # type: ignore