本文整理汇总了Python中discord.Reaction方法的典型用法代码示例。如果您正苦于以下问题:Python discord.Reaction方法的具体用法?Python discord.Reaction怎么用?Python discord.Reaction使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类discord
的用法示例。
在下文中一共展示了discord.Reaction方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: help_cleanup
# 需要导入模块: import discord [as 别名]
# 或者: from discord import Reaction [as 别名]
def help_cleanup(bot: Bot, author: Member, message: Message) -> None:
"""
Runs the cleanup for the help command.
Adds the :trashcan: reaction that, when clicked, will delete the help message.
After a 300 second timeout, the reaction will be removed.
"""
def check(reaction: Reaction, user: User) -> bool:
"""Checks the reaction is :trashcan:, the author is original author and messages are the same."""
return str(reaction) == DELETE_EMOJI and user.id == author.id and reaction.message.id == message.id
await message.add_reaction(DELETE_EMOJI)
try:
await bot.wait_for("reaction_add", check=check, timeout=300)
await message.delete()
except TimeoutError:
await message.remove_reaction(DELETE_EMOJI, bot.user)
except NotFound:
pass
示例2: react_check
# 需要导入模块: import discord [as 别名]
# 或者: from discord import Reaction [as 别名]
def react_check(self, reaction: Reaction, user: User) -> bool:
"""
Parameters
----------
reaction : Reaction
The `Reaction` object of the reaction.
user : User
The `User` or `Member` object of who sent the reaction.
Returns
-------
bool
"""
return (
reaction.message.id == self.base.id
and user.id == self.ctx.author.id
and reaction.emoji in self.reaction_map.keys()
)
示例3: emoji_choice
# 需要导入模块: import discord [as 别名]
# 或者: from discord import Reaction [as 别名]
def emoji_choice(self, text: str, choices: Collection[str]):
emoji = ''
while emoji not in choices:
mes: Message = await self.ctx.send(
embed=Embed(
color=Color.blurple(),
description=text))
for choice in choices:
await mes.add_reaction(choice)
def check(reaction: Reaction, user: User):
message: Message = reaction.message
return message.channel == self.message.channel and user.id == self.message.author.id
try:
reaction, user = await self.bot.wait_for('reaction_add', check=check, timeout=30)
emoji = str(reaction.emoji)
except asyncio.TimeoutError:
raise AwaitTimedOut
return emoji
示例4: on_reaction_add
# 需要导入模块: import discord [as 别名]
# 或者: from discord import Reaction [as 别名]
def on_reaction_add(self, reaction: Reaction, user):
if self.__handle(reaction.message.id, user.id, reaction.emoji, True,
False):
# print("Already handled")
return
# print("Handling")
await self.voter.handle_reaction(reaction, user, True)
示例5: predicate_eval_emoji_reaction
# 需要导入模块: import discord [as 别名]
# 或者: from discord import Reaction [as 别名]
def predicate_eval_emoji_reaction(ctx: Context, reaction: Reaction, user: User) -> bool:
"""Return True if the reaction REEVAL_EMOJI was added by the context message author on this message."""
return reaction.message.id == ctx.message.id and user.id == ctx.author.id and str(reaction) == REEVAL_EMOJI
示例6: _reaction_check
# 需要导入模块: import discord [as 别名]
# 或者: from discord import Reaction [as 别名]
def _reaction_check(
self,
author: Member,
message: Message,
reaction: Reaction,
user: t.Union[Member, User]
) -> bool:
"""
Return True if the `reaction` is a valid confirmation or abort reaction on `message`.
If the `author` of the prompt is a bot, then a reaction by any core developer will be
considered valid. Otherwise, the author of the reaction (`user`) will have to be the
`author` of the prompt.
"""
# For automatic syncs, check for the core dev role instead of an exact author
has_role = any(constants.Roles.core_developers == role.id for role in user.roles)
return (
reaction.message.id == message.id
and not user.bot
and (has_role if author.bot else user == author)
and str(reaction.emoji) in self._REACTION_EMOJIS
)
示例7: wait_for_deletion
# 需要导入模块: import discord [as 别名]
# 或者: from discord import Reaction [as 别名]
def wait_for_deletion(
message: Message,
user_ids: Sequence[Snowflake],
deletion_emojis: Sequence[str] = (Emojis.trashcan,),
timeout: float = 60 * 5,
attach_emojis: bool = True,
client: Optional[Client] = None
) -> None:
"""
Wait for up to `timeout` seconds for a reaction by any of the specified `user_ids` to delete the message.
An `attach_emojis` bool may be specified to determine whether to attach the given
`deletion_emojis` to the message in the given `context`
A `client` instance may be optionally specified, otherwise client will be taken from the
guild of the message.
"""
if message.guild is None and client is None:
raise ValueError("Message must be sent on a guild")
bot = client or message.guild.me
if attach_emojis:
for emoji in deletion_emojis:
await message.add_reaction(emoji)
def check(reaction: Reaction, user: Member) -> bool:
"""Check that the deletion emoji is reacted by the appropriate user."""
return (
reaction.message.id == message.id
and str(reaction.emoji) in deletion_emojis
and user.id in user_ids
)
with contextlib.suppress(asyncio.TimeoutError):
await bot.wait_for('reaction_add', check=check, timeout=timeout)
await message.delete()
示例8: add_reaction
# 需要导入模块: import discord [as 别名]
# 或者: from discord import Reaction [as 别名]
def add_reaction(msg, reaction: discord.Reaction) -> bool:
if reaction != "disable":
try:
await msg.add_reaction(reaction)
except (discord.HTTPException, discord.InvalidArgument) as e:
logger.warning("Failed to add reaction %s: %s.", reaction, e)
return False
return True
示例9: on_reaction_remove
# 需要导入模块: import discord [as 别名]
# 或者: from discord import Reaction [as 别名]
def on_reaction_remove(self, reaction: Reaction, user):
if self.__handle(reaction.message.id, user.id, reaction.emoji, False,
False):
# print("Already handled")
return
# print("Handling")
await self.voter.handle_reaction(reaction, user, False)
示例10: on_reaction_add
# 需要导入模块: import discord [as 别名]
# 或者: from discord import Reaction [as 别名]
def on_reaction_add(self, reaction, user):
"""
Starts events when a user adds an emote reaction to a message.
This event is triggered only if the message that had a reaction removed is cached.
:type reaction: discord.Reaction
:type user: discord.User
:param reaction: The reaction that was added to the message.
:param user: The user that added the reaction.
:return:
"""
if not user.bot:
payload = ReactionPayload(self, reaction, user)
self.loop.create_task(self.queue.event_runner('reaction_add', payload))
if str(reaction.emoji) in ['⬅', '➡']:
self.loop.create_task(self.queue.event_runner('paginate', payload))
示例11: on_reaction_remove
# 需要导入模块: import discord [as 别名]
# 或者: from discord import Reaction [as 别名]
def on_reaction_remove(self, reaction, user):
"""
Starts events when a user removes an emote reaction from a message.
This event is triggered only if the message that had a reaction removed is cached.
:type reaction: discord.Reaction
:type user: discord.User
:param reaction: The reaction that was removed from the message.
:param user: The user that removed the reaction.
:return:
"""
if not user.bot:
payload = ReactionPayload(self, reaction, user)
self.loop.create_task(self.queue.event_runner('reaction_remove', payload))
示例12: attempt_add_reaction
# 需要导入模块: import discord [as 别名]
# 或者: from discord import Reaction [as 别名]
def attempt_add_reaction(msg: discord.Message, reaction: typing.Union[str, discord.Emoji])\
-> typing.Optional[discord.Reaction]:
"""
Try to add a reaction to a message, ignoring it if it fails for any reason.
:param msg: The message to add the reaction to.
:param reaction: The reaction emoji, could be a string or `discord.Emoji`
:return: A `discord.Reaction` or None, depending on if it failed or not.
"""
try:
return await msg.add_reaction(reaction)
except discord.HTTPException:
pass
示例13: predicate
# 需要导入模块: import discord [as 别名]
# 或者: from discord import Reaction [as 别名]
def predicate(
self,
ctx: commands.Context,
announcement: discord.Message,
reaction: discord.Reaction,
user: discord.Member
) -> bool:
"""Predicate checking the criteria for the announcement message."""
if self.already_playing(ctx.author): # If they've joined a game since requesting a player 2
return True # Is dealt with later on
if (
user.id not in (ctx.me.id, ctx.author.id)
and str(reaction.emoji) == HAND_RAISED_EMOJI
and reaction.message.id == announcement.id
):
if self.already_playing(user):
self.bot.loop.create_task(ctx.send(f"{user.mention} You're already playing a game!"))
self.bot.loop.create_task(announcement.remove_reaction(reaction, user))
return False
if user in self.waiting:
self.bot.loop.create_task(ctx.send(
f"{user.mention} Please cancel your game first before joining another one."
))
self.bot.loop.create_task(announcement.remove_reaction(reaction, user))
return False
return True
if (
user.id == ctx.author.id
and str(reaction.emoji) == CROSS_EMOJI
and reaction.message.id == announcement.id
):
return True
return False
示例14: _validate_answer
# 需要导入模块: import discord [as 别名]
# 或者: from discord import Reaction [as 别名]
def _validate_answer(self, ctx: Context, message: Message, answer: str, options: list) -> None:
"""Validate the answer using a reaction event loop."""
def predicate(reaction: Reaction, user: Member) -> bool:
"""Test if the the answer is valid and can be evaluated."""
return (
reaction.message.id == message.id # The reaction is attached to the question we asked.
and user == ctx.author # It's the user who triggered the quiz.
and str(reaction.emoji) in ANSWERS_EMOJI.values() # The reaction is one of the options.
)
for emoji in ANSWERS_EMOJI.values():
await message.add_reaction(emoji)
# Validate the answer
try:
reaction, user = await ctx.bot.wait_for("reaction_add", timeout=45.0, check=predicate)
except asyncio.TimeoutError:
await ctx.channel.send(f"You took too long. The correct answer was **{options[answer]}**.")
await message.clear_reactions()
return
if str(reaction.emoji) == ANSWERS_EMOJI[answer]:
await ctx.send(f"{random.choice(CORRECT_GUESS)} The correct answer was **{options[answer]}**.")
else:
await ctx.send(
f"{random.choice(INCORRECT_GUESS)} The correct answer was **{options[answer]}**."
)
await message.clear_reactions()
# endregion
# region: Commands
示例15: on_reaction_add
# 需要导入模块: import discord [as 别名]
# 或者: from discord import Reaction [as 别名]
def on_reaction_add(self, reaction: Reaction, user: User) -> None:
"""Event handler for when reactions are added on the help message."""
# ensure it was the relevant session message
if reaction.message.id != self.message.id:
return
# ensure it was the session author who reacted
if user.id != self.author.id:
return
emoji = str(reaction.emoji)
# check if valid action
if emoji not in REACTIONS:
return
self.reset_timeout()
# Run relevant action method
action = getattr(self, f'do_{REACTIONS[emoji]}', None)
if action:
await action()
# remove the added reaction to prep for re-use
with suppress(HTTPException):
await self.message.remove_reaction(reaction, user)