【问题标题】:Python Discord Bot Comparing Message to ListPython Discord Bot 将消息与列表进行比较
【发布时间】:2018-02-22 22:25:30
【问题描述】:

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

操作系统: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)

【问题讨论】:

  • 请发布导致问题的简洁代码部分,而不是仅仅粘贴整个程序。

标签: python bots chatbot discord discord.py


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2020-08-24
    • 2021-03-27
    • 2021-10-30
    • 1970-01-01
    • 2018-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-04
    相关资源
    最近更新 更多