首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在Python语言中创建不和谐的机器人:如何为ChatBot创建切换对话

在Python语言中创建不和谐的机器人:如何为ChatBot创建切换对话
EN

Stack Overflow用户
提问于 2018-07-29 08:35:05
回答 1查看 619关注 0票数 1

我试图在Python语言中创建一个不和谐的ChatBot,但我似乎遇到了一个问题。我做了一个开关来让它说话,但每当我切换开关时,它就会说' talk‘不能被识别为命令。我甚至试着用@client.event代替@client.command,但是不起作用。请帮帮忙。

代码语言:javascript
复制
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
async def talk(message):
    global on_talk
    if message.content.upper().startswith(":TALK"):
        on_talk = True
    if message.content.upper().startswith(":STOPTALK"):
        on_talk = False
if on_talk == False:
    print("on_talk is set to False")
if on_talk == True:
    print("It works")

编辑: talk函数可以工作,但我似乎不能让stoptalk函数工作。我试过这么做。

代码语言:javascript
复制
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):
    everything i want to do

有人能帮上忙吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-29 08:42:43

client.command返回一个装饰器,所以在装饰协例程时必须调用它。如果您想要传递消息,则必须指定pass_context=True并通过ctx.message访问消息。

代码语言:javascript
复制
@client.command(pass_context=True)
async def talk(ctx):
    global on_talk
    on_talk = True


@client.command(pass_context=True)
async def stoptalk(ctx):
    global on_talk
    on_talk = False
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51576304

复制
相关文章

相似问题

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