我试图在Python语言中创建一个不和谐的ChatBot,但我似乎遇到了一个问题。我做了一个开关来让它说话,但每当我切换开关时,它就会说' talk‘不能被识别为命令。我甚至试着用@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")
编辑: talk函数可以工作,但我似乎不能让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
有人能帮上忙吗?
发布于 2018-07-29 08:42:43
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
https://stackoverflow.com/questions/51576304
复制相似问题