2017-09-14 3 views
0

私はDiscord Python APIを使ってPython Discord Botを作っています。私は彼らがイベントの現在のリストにevent_add {メッセージ/彼らが追加したいイベント}をメッセージした後にメッセージを比較しようとしています。メッセージが現在のイベントリストと一致する場合、ボットは既にイベントがある旨のメッセージを返します。私の問題は、文字列がリストと比較したくないということであり、常に一致しないことを返します。Python Discord Botメッセージをリストと比較する

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) 
+0

プログラム全体を貼り付けるのではなく、問題の原因となっているコードの簡潔な部分を投稿してください。 – squaswin

答えて

0

あなたがしているように見えますグローバルスコープのeventsをセットとして定義し、test()にリダイレクトしようとしています。 test()で定義された

eventsあなたはadd_event()で使用しようとしているeventsは何にを持っていないグローバルスコープの1、であるのに対し、それは、関数呼び出しの終了時に削除されますつまり、ローカルスコープにありますtest()のものとしてください。

とにかく修正するには、test()の先頭にglobal eventsを追加してください。これは、eventsを再定義すると、既にグローバルなものを置き換えることを意味します。

関連する問題