如何为最先反应的用户添加冷却映射?
How to add cooldown mapping to users who reacted first?
我目前正在开发一个购物车管理器,用户有机会获得赠品(先到先得)。基本上我会自动 post 一些嵌入,第一个做出反应的人将获得奖品和机器人用 DM 写的消息。最先获得奖品的用户将获得5分钟的冷却时间(这是因为同一用户在5分钟内应该无法获得二等奖)
我写过这样的东西:
# -*- coding: utf-8 -*-
import asyncio
from datetime import datetime
from discord import Client, Embed, Object
client = Client()
lock = asyncio.Lock()
PRIVATE_CHANNEL_ID = xxxxxxxxxxxxxx
PUBLIC_CHANNEL_ID = xxxxxxxxxxxxx
EMOJI_ID = "\N{SHOPPING TROLLEY}"
# ----------------------------------------------------- #
@client.event
async def on_ready():
print(f'{client.user.name} Logged In!')
@client.event
async def on_raw_reaction_add(payload):
if payload.channel_id == PUBLIC_CHANNEL_ID and '\U0001f6d2' == str(payload.emoji) and payload.user_id != client.user.id:
async with lock:
# Check if person is rate limited
我已经阅读了一些关于冷却时间映射的文章,并找到了一个例子:
class SomeCog(commands.Cog):
def __init__(self):
self._cd = commands.CooldownMapping.from_cooldown(1.0, 60.0, commands.BucketType.user)
async def cog_check(self, ctx):
bucket = self._cd.get_bucket(ctx.message)
retry_after = bucket.update_rate_limit()
if retry_after:
# you're rate limited
# helpful message here
pass
# you're not rate limited
但是我的问题是我不知道如何为首先对给定反应做出反应的用户应用冷却时间,我想知道我该怎么做?如何使用 CooldownMapping
?
向用户应用冷却时间,使其在接下来的 5 分钟内无法做出反应
您的代码展示了如何在 cog 中的命令之间设置通用速率限制,要在 on_raw_reaction_add
事件上设置冷却时间,您需要不同的方法。
raw_reaction_add_cooldown = commands.CooldownMapping.from_cooldown(
1, 60.0, commands.BucketType.user # change rate and per accordingly
)
async def get_raw_reaction_add_ratelimit(payload: discord.RawReactionActionEvent) -> Optional[float]:
guild = client.get_guild(payload.guild_id)
channel = client.get_channel(payload.channel_id)
if guild is None or channel is None:
return None
author = guild.get_member(payload.user_id)
message = await channel.fetch_message(payload.message_id)
if author is None or message is None:
return None
# overwriting author attribute since otherwise it will check the ratelimit
# of the user who SENT the message, not the one that reacted
message.author = author
bucket = raw_reaction_add_cooldown.get_bucket(message)
return bucket.update_rate_limit()
要使用它非常简单,调用函数并检查 None
@client.event
async def on_raw_reaction_add(payload):
ratelimit = await get_raw_reaction_add_ratelimit(payload)
if ratelimit is None:
print("NO RATELIMIT")
else:
print("RATELIMIT")
我目前正在开发一个购物车管理器,用户有机会获得赠品(先到先得)。基本上我会自动 post 一些嵌入,第一个做出反应的人将获得奖品和机器人用 DM 写的消息。最先获得奖品的用户将获得5分钟的冷却时间(这是因为同一用户在5分钟内应该无法获得二等奖)
我写过这样的东西:
# -*- coding: utf-8 -*-
import asyncio
from datetime import datetime
from discord import Client, Embed, Object
client = Client()
lock = asyncio.Lock()
PRIVATE_CHANNEL_ID = xxxxxxxxxxxxxx
PUBLIC_CHANNEL_ID = xxxxxxxxxxxxx
EMOJI_ID = "\N{SHOPPING TROLLEY}"
# ----------------------------------------------------- #
@client.event
async def on_ready():
print(f'{client.user.name} Logged In!')
@client.event
async def on_raw_reaction_add(payload):
if payload.channel_id == PUBLIC_CHANNEL_ID and '\U0001f6d2' == str(payload.emoji) and payload.user_id != client.user.id:
async with lock:
# Check if person is rate limited
我已经阅读了一些关于冷却时间映射的文章,并找到了一个例子:
class SomeCog(commands.Cog):
def __init__(self):
self._cd = commands.CooldownMapping.from_cooldown(1.0, 60.0, commands.BucketType.user)
async def cog_check(self, ctx):
bucket = self._cd.get_bucket(ctx.message)
retry_after = bucket.update_rate_limit()
if retry_after:
# you're rate limited
# helpful message here
pass
# you're not rate limited
但是我的问题是我不知道如何为首先对给定反应做出反应的用户应用冷却时间,我想知道我该怎么做?如何使用 CooldownMapping
?
您的代码展示了如何在 cog 中的命令之间设置通用速率限制,要在 on_raw_reaction_add
事件上设置冷却时间,您需要不同的方法。
raw_reaction_add_cooldown = commands.CooldownMapping.from_cooldown(
1, 60.0, commands.BucketType.user # change rate and per accordingly
)
async def get_raw_reaction_add_ratelimit(payload: discord.RawReactionActionEvent) -> Optional[float]:
guild = client.get_guild(payload.guild_id)
channel = client.get_channel(payload.channel_id)
if guild is None or channel is None:
return None
author = guild.get_member(payload.user_id)
message = await channel.fetch_message(payload.message_id)
if author is None or message is None:
return None
# overwriting author attribute since otherwise it will check the ratelimit
# of the user who SENT the message, not the one that reacted
message.author = author
bucket = raw_reaction_add_cooldown.get_bucket(message)
return bucket.update_rate_limit()
要使用它非常简单,调用函数并检查 None
@client.event
async def on_raw_reaction_add(payload):
ratelimit = await get_raw_reaction_add_ratelimit(payload)
if ratelimit is None:
print("NO RATELIMIT")
else:
print("RATELIMIT")