Python Discord 机器人将消息与列表进行比较

Python Discord Bot Comparing Message to List

好的,我正在使用 Discord python API 制作 python Discord 机器人。在他们向当前事件列表发送命令 ?event_add {message/event they want to add} 后,我正在尝试比较消息。如果该消息与当前事件列表相匹配,则机器人会 return 并显示一条消息,说明我们已经有了该事件。我的问题是该字符串不想与列表进行比较,并且总是 return 返回它不匹配。

OS : Windows 10 个创作者更新

Python : 3.6.2

Discord.py : https://discordpy.readthedocs.io/en/latest/, GitHub : https://github.com/Rapptz/discord.py

代码:

import discord
from discord.ext import commands
import logging
import sys
import time
import asyncio

bot = commands.Bot(command_prefix="/")
console = discord.Object("357208549614419970")
events = {"learn to bake"}


@bot.event
async def on_ready():
    print("Logged in as: ")
    print(bot.user.id)
    print(bot.user.name)
    print("******************")

@bot.command(pass_context = True)
async def test(ctx):
    await bot.say("Testing...... Am I a real boy yet?")
    events = ['drawn out a dragon, and do a hand stand']
    await bot.say(events)

@bot.command(pass_context = True)
async def add_event(ctx, event):
    if event in events:
        await bot.say("Sorry we already have that, also we need to teach %s 
to read. Add that to the list please." % ctx.message.author.mention)
    else:
        await bot.say("Something is broken %s" % ctx.message.author.mention)

看起来您在全局范围内将 events 定义为一个集合,然后尝试在 test() 中重新定义它。

test() 中定义的 events 在局部范围内,这意味着它会在函数调用结束时被删除,而你试图在中使用的 events add_event()是全局范围内的,与test().

中的无关

无论如何,要修复它,只需在 test() 的顶部添加一个 global events。这将意味着当您重新定义 events 时,您将替换已经全局的。