首页 文章

Python Discord Bot比较消息到列表

提问于
浏览
0

好吧我正在使用Discord python API制作一个python Discord Bot . 我正在尝试比较消息后他们将命令?event_add {message / event他们想要添加}发送到当前事件列表 . 如果消息与当前事件列表匹配,则机器人将返回一条消息,表明我们已经拥有该事件 . 我的问题是字符串不想与列表进行比较,并始终返回它不匹配的内容 .

操作系统:Windows 10 Creators更新

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)

1 回答

  • 0

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

    test() 中定义的 events 在本地范围内,这意味着它将在函数调用结束时删除,而您尝试在 add_event() 中使用的 events 是全局范围内的 events ,这与一个范围无关 . 在 test() .

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

相关问题