python - 如何让我的机器人在没有 on_message 事件的情况下响应用户提及?
问题描述
块引用
@client.command()
async def afk(ctx, activity=None):
if ctx.author.mention:
await ctx.send(f"""{ctx.author.mention} is currently afk. Reason: {activity}""")
else:
print("A user is afk...")
我正在尝试制作一个 afk 命令,这就是我所得到的。我无法让我的机器人响应最近刚刚使用此 afk 命令的用户。基本上我正在尝试制作 Dyno 机器人的 afk 命令。
"The AFK module allows users to set an AFK status inside of your server. If a user sets an AFK status, Dyno will leave a message displaying their AFK status whenever the user is mentioned. Dyno will automatically add “[AFK]” to the beginning of your nickname whenever you set an AFK status (if it has the permissions to)."
这是 Dyno 的 afk 命令的定义。这基本上也是我想要做的。
解决方案
您可能试图在不了解实际库的情况下复制其他创建者。您需要使用自己的逻辑并理解这些概念。
我不知道 dyno 是如何工作的,但是,我已经理解您实际上想说的是什么,这个片段可以让您了解事情是如何工作的以及如何在代码中实现它们。
#STORING THE USER IN A LIST
afk_list = []
@bot.command()
async def afk(ctx):
global afk_list
# IF USER ID IS NOT FOUND IN THE LIST
if not ctx.author.id in afk_list:
#STORE THE ID IN THE LIST
afk_list.append(ctx.author.id)
#EDIT THE NAME OF THE AUTHOR
await ctx.author.edit(nick = f'[AFK] {ctx.author.name}')
await ctx.send(f'{ctx.author.mention} You have been marked as AFK.')
return afk_list
else:
afk_list.remove(ctx.author.id)
nick = ctx.author.name.replace('[AFK]','')
await ctx.author.edit(nick = nick)
await ctx.send(f'{ctx.author.mention} You are no longer AFK.')
return afk_list
@bot.event
async def on_message(message):
global afk_list
try:
#We check if the message starts with any mentionable content
if message.content.startswith('<'):
a = message.content
a = a.replace('<','')
a = a.replace('@','')
a = a.replace('!','')
a = a.replace('>','')
a = int(a)
#HERE We have replaced all the strings and converted it into Integar.
if a in afk_list:
#If the User in the LIST, The message will be sent.
user = bot.get_user(a)
await message.channel.send(f'{user.name} is currently AFK')
else:
pass
except Exception:
pass
await bot.process_commands(message)
推荐阅读
- javascript - Vue:应用过滤器后如何清除过滤器?
- docker - Traefik v2:将 http://servername/foo 重定向到 http://servername:port
- android-databinding - Android Studio 不会创建绑定类
- logic - 将一阶逻辑 (FOL) 转换为 CNFFormula
- javascript - 两个数组的最小值以创建一个新数组
- c# - 从字符串中删除前导零
- reactjs - 带有 react-google-maps API 的谷歌地图
- java - 如何跳过某个数字并继续?
- java - 使用 Selenium WebDriver 的新选项卡
- python - 我想计算数据框中的 python 列表的元素,并将输出作为数据框中的一列。我怎么做?