首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在Python中制造一个不和谐的机器人:为什么我的函数不工作?

在Python中制造一个不和谐的机器人:为什么我的函数不工作?
EN

Stack Overflow用户
提问于 2018-07-30 06:08:37
回答 1查看 293关注 0票数 0

请记住,我是Python的新手,也是discord的新手。我正在尝试使用Python创建一个ChatBot,我有一个命令,你必须说出来才能让机器人和你说话。该命令称为talk。要停止与机器人的对话,您必须启用stoptalk。遗憾的是,我编写的启用stop talk的函数不起作用,我不知道如何修复它。请帮帮忙。

import discord
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio
import time
Client = discord.Client()
client = commands.Bot(command_prefix = ":")
on_talk = False
 @client.event
async def on_ready():
    print("You can talk to me now!")
@client.command(pass_context=True)
async def ontalker(message):
    global on_talk
    if message.content.upper().startswith(":TALK"):
        on_talk = True

@client.command(pass_context=True)
async def offtalker(message):    
    global ontalk
    if message.content.upper().startswith(":STOPTALK"):
        on_talk = False


@client.command(pass_context=True)
async def stoptalk(ctx):
    print("on_talk is False.")

@client.command(pass_context=True)
async def talk(ctx):
    @client.event
    # Everything I want to do goes here
EN

回答 1

Stack Overflow用户

发布于 2018-07-30 06:12:05

您的offtalker()函数中有一个拼写错误。你写道:

global ontalk

但是,您尝试修改的全局变量是on_talk,因此该变量仅在函数的局部作用域中更改为False。相反,可以这样写:

global on_talk
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51584954

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档