我目前正在尝试使用python(对我来说几乎是全新的东西)在Discord中创建一个机器人。我试图制作一个AFK函数(类似于Dyno的)。这是我的代码:
afkdict = {}
@client.command(name = "afk", brief = "Away From Keyboard",
description = "I'll give you the afk status and if someone pings you before you come back, I'll tell "
"them that you are not available. You can add your own afk message!")
async def afk(ctx, message = "They didn't leave a message!"):
global afkdict
if ctx.message.author in afkdict:
afkdict.pop(ctx.message.author)
await ctx.send('Welcome back! You are no longer afk.')
else:
afkdict[ctx.message.author] = message
await ctx.send("You are now afk. Beware of the real world!")
@client.event
async def on_message(message):
global afkdict
for member in message.mentions:
if member != message.author:
if member in afkdict:
afkmsg = afkdict[member]
await message.channel.send(f"Oh noes! {member} is afk. {afkmsg}")
await client.process_commands(message)
我这里的问题是,使用此函数的用户在再次写入之前都是AFK>afk
。我的意图是,无论用户是否使用bot功能,只要用户再次在服务器上讲话,bot都能够删除AFK状态。我知道这是可能的,因为其他机器人都这么做了,但我迷路了,想不出一个办法。
提前谢谢
我认为应该这样做,但您也可以再次传递ctx或将ctx保存为参数以访问。 但是这个版本不会通知用户,他们不再是afk了
相关问题 更多 >
编程相关推荐