请记住,我是Python的新手,也是不和谐API的新手。我正在尝试使用Discord中的Python制作ChatBot,并且我有一条命令必须让机器人与您对话。该命令称为通话。要停止与机器人通话,您必须启用Stoptalk。遗憾的是,我编写的用于启用停止通话的功能不起作用,并且我不知道该如何解决。请帮忙。
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
答案 0 :(得分:0)
您的offtalker()
函数中有错字。您写道:
global ontalk
但是,您尝试修改的全局变量为on_talk
,因此在函数的本地作用域中变量仅更改为False
。而是写:
global on_talk