如何检查Discord机器人是否是输入命令的用户 [英] How to check if discord bot is the user entering a command
本文介绍了如何检查Discord机器人是否是输入命令的用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码
import discord
import asyncio
import sys
import commands
import pickle
import os
import random
import array
#if len(sys.argv) != 2:
# print('Usage: python3 main.py [token]')
commandz=("yes","no","hi","hi")
try:
with open(os.path.join(os.path.dirname(__file__), "token.pickle"), 'rb') as file:
token = pickle.load(file)
if len(token) == 59:
key = int(random.random() * 10000000000000000)
print('Found saved token in stored.py, use phrase tokenreset'+str(key), 'to undo this.')#youll need to add the command
else:
raise
except:
#code tokenreset with admin permissions
token = input('What is your Discord bot token? (found on Discord developer page): ')
with open(os.path.join(os.path.dirname(__file__), "token.pickle"), 'wb') as file:
pickle.dump(token, file)
client = discord.Client()
@client.event
async def on_ready():
print('Logged in as' + '[' + client.user.id + ']' + client.user.name)
print('--------')
@client.event
async def on_message(message):
await handle_command(message)
async def handle_command(message):
print('Noticed: ' + message.content)
if message.content == 'tokenreset'+str(key):
await client.send_message(message.channel, 'code accepted')
i = 0
if str(message.author) == "NOTAKOALAINVENEZUELA#6895":
i = 1
x = 0
await client.send_message(message.channel, message.author)
while i == 0:
if commandz[x] in message.content:
x = x + 1
await client.send_message(message.channel, commandz[x])
i = i + 1
else:
if x == len(commandz) - 2:
i = i + 1
else:
x = x + 2
client.run(token)
The结果是我的机器人一遍又一遍地触发了自己的命令,我尽了一切努力使其无法识别自己的消息,而我处于机智。
The result is my bot triggering its own commands over and over, I have tried everything to get it to not recognize it's own messages and I am at my wits end.
出于某种原因message.author是无法在比较中使用的某种奇怪的变量,我对编程和python来说是超级新手,所以不确定。
I think that for some reason message.author is some weird kind of variable that cant be used in a comparison, I am super new to programming and python so I'm not sure.
推荐答案
您可以尝试使用以下方法检查作者是否是机器人:
You can try to check if the author is the bot using this:
if message.author.id === message.server.me.id
这样您可以确定该机器人不会自行触发。
That way you can be sure the bot doesn't trigger itself.
这篇关于如何检查Discord机器人是否是输入命令的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文