使用Python制作不和谐机器人:如何为ChatBot进行切换对话

时间:2018-07-29 00:35:05

标签: python discord discord.py

我正在尝试用Python创建一个不一致的ChatBot,但似乎遇到了一个问题。我正在拨动开/关开关以使其说话,但是每当拨动该开关时,它都说“通话”未被识别为命令。我什至尝试将其设置为@ client.event而不是@ client.command,但它不起作用。请帮忙。

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")

编辑:对话功能有效,但我似乎无法使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):
    everything i want to do

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

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

@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