【问题标题】:How to check if discord bot is the user entering a command如何检查不和谐机器人是否是用户输入命令
【发布时间】:2018-03-17 18:56:24
【问题描述】:

这是我的代码

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)

结果是我的机器人一遍又一遍地触发自己的命令,我已经尽一切努力让它无法识别自己的消息,而我束手无策。

我认为由于某种原因 message.author 是一种无法用于比较的奇怪变量,我对编程和 python 非常陌生,所以我不确定。

【问题讨论】:

  • 您发布的代码不会运行。请仔细检查需要自己的行的所有内容是否都有,并且所有内容都正确缩进。
  • 是的,当发布格式混乱我编辑

标签: python discord


【解决方案1】:

你可以尝试使用这个来检查作者是否是机器人:

if message.author.id === message.server.me.id

这样您就可以确保机器人不会自行触发。

【讨论】:

  • @trisimix 你看过github页面的examples文件夹了吗? github.com/Rapptz/discord.py/blob/async/examples/reply.py
  • @trisimix,我将它用于我的机器人,但我认为您可以在示例或一些随机教程中看到它。另外,如果它有效,请随时给我正确的答案 :)
猜你喜欢
  • 2021-08-04
  • 1970-01-01
  • 2020-02-16
  • 2019-11-05
  • 1970-01-01
  • 2018-11-10
  • 2018-07-21
  • 2019-06-30
  • 2021-03-07
相关资源
最近更新 更多