Python Discord Bot比较邮件列表
问题描述:
好吧我正在使用Discord python API制作python Discord Bot。我试图在他们发送命令event_add {他们想要添加的消息/事件}到当前事件列表之后比较消息。如果消息与当前的事件列表匹配,机器人将返回一条消息,说明我们已经拥有该事件。我的问题是,字符串不想与列表进行比较,并总是返回它不匹配。Python Discord Bot比较邮件列表
操作系统: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
是在局部范围,这意味着它将在函数调用结束时删除,而你试图在add_event()
使用events
是一个在全球范围内,它没有任何关系与test()
中的那个一起做。
无论如何,要解决它,只需将global events
添加到test()
的顶部。这意味着当您重新定义events
时,您将替换已经是全球的一个。
请发布导致问题的简要代码段,而不是仅粘贴整个程序。 – squaswin