Python Discord Bot将消息与列表进行比较

Python Discord Bot将消息与列表进行比较,python,bots,chatbot,discord,discord.py,Python,Bots,Chatbot,Discord,Discord.py,好的,我正在使用Discord python API制作一个python Discord机器人。我试图在他们将命令?event_add{message/event this want add}发送到当前事件列表后比较消息。如果消息与当前事件列表匹配,则bot将返回一条消息,说明我们已经拥有该事件。我的问题是字符串不想与列表进行比较,并且总是返回不匹配的字符串 操作系统:Windows 10创建者更新 Python:3.6.2 Discord.py:,GitHub: 代码: 看起来您正在将全局作用

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

操作系统:Windows 10创建者更新

Python:3.6.2

Discord.py:,GitHub:

代码:


看起来您正在将全局作用域上的
事件定义为一个集合,然后尝试在
test()
中重新定义它

test()
中定义的
events
位于本地作用域上,这意味着它将在函数调用结束时被删除,而在
add\u event()
中尝试使用的
events
则位于全局作用域上,与
test()
中的事件无关


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

请发布导致问题的简明代码部分,而不仅仅是粘贴整个程序。
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)