我目前正在尝试使用 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.eventasync 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。我的目的是让机器人能够在用户再次在服务器中通话时删除 AFK 状态,无论他们是否使用机器人功能。我知道这是可能的,因为其他机器人也这样做,但我迷失了方向,想不出办法。提前致谢!
1 回答
青春有我
TA贡献1784条经验 获得超8个赞
async def on_message(message):
global afkdict
if message.author in afkdict:
afkdict.pop(message.author)
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)
我认为应该这样做,但是您也可以再次传递 ctx 或将 ctx 保存为参数来访问。但这个版本并没有通知用户,他们不再afk了
添加回答
举报
0/150
提交
取消