如何阻止 discord 机器人响应 itself/all 其他机器人 [Python 3.6 中的 Discord Bot]
How to stop discord bot respond to itself/all other bots [Discord Bot in Python 3.6]
我对编程很陌生,只是在学习。我认为制作一个不和谐的机器人是一种很好的学习方式,我很享受,只是有点卡住了。所以我的机器人是私有的,我们的 discord 服务器中有一个 运行 笑话,每当用户发送 "k" 时,所有机器人都会响应 "k"。 ATM 我们有 Dyno,我朋友的私人机器人,希望也是我的。我的所有代码都可以正常工作,除了因为命令和答案相同我的机器人只是用 "k" 向服务器发送垃圾邮件,直到我关闭机器人,我该如何停止它?
代码:
@client.event
async def on_message(message):
if message.content ==("k"):
await client.send_message(message.channel, "k")
await client.process_commands(message)
您可以简单地 运行 在您的消息事件中这样做:
if(!message.author.user.bot) return; //not the exact code
因为消息事件可以return
用户,而用户有一个默认bot check. message.author
returns a member, so you can call the user
object of member然后像我上面那样执行检查
伪代码:
If the author of the message (user) is a bot: return;
This will prevent an infinite loop from occuring
没关系,我终于想通了。对于那些有同样问题的人,你必须添加
if message.author == client.user:
return
if message.author.bot: return
代码和它的工作所以我现在看起来像这样:
@client.event
async def on_message(message):
# we do not want the bot to reply to itself
if message.author == client.user:
return
if message.author.bot: return
if message.content.startswith('k'):
msg = 'k'.format(message)
await client.send_message(message.channel, msg)
你可以在js中使用这个-->
if (message.author.bot == false){
//code here if the user isn't a bot
}
//lol i am noob yet
我对编程很陌生,只是在学习。我认为制作一个不和谐的机器人是一种很好的学习方式,我很享受,只是有点卡住了。所以我的机器人是私有的,我们的 discord 服务器中有一个 运行 笑话,每当用户发送 "k" 时,所有机器人都会响应 "k"。 ATM 我们有 Dyno,我朋友的私人机器人,希望也是我的。我的所有代码都可以正常工作,除了因为命令和答案相同我的机器人只是用 "k" 向服务器发送垃圾邮件,直到我关闭机器人,我该如何停止它?
代码:
@client.event
async def on_message(message):
if message.content ==("k"):
await client.send_message(message.channel, "k")
await client.process_commands(message)
您可以简单地 运行 在您的消息事件中这样做:
if(!message.author.user.bot) return; //not the exact code
因为消息事件可以return
用户,而用户有一个默认bot check. message.author
returns a member, so you can call the user
object of member然后像我上面那样执行检查
伪代码:
If the author of the message (user) is a bot: return;
This will prevent an infinite loop from occuring
没关系,我终于想通了。对于那些有同样问题的人,你必须添加
if message.author == client.user:
return
if message.author.bot: return
代码和它的工作所以我现在看起来像这样:
@client.event
async def on_message(message):
# we do not want the bot to reply to itself
if message.author == client.user:
return
if message.author.bot: return
if message.content.startswith('k'):
msg = 'k'.format(message)
await client.send_message(message.channel, msg)
你可以在js中使用这个-->
if (message.author.bot == false){
//code here if the user isn't a bot
}
//lol i am noob yet