【问题标题】:Making a Discord Bot in Python: Why is my function not working?在 Python 中制作 Discord Bot:为什么我的函数不起作用?
【发布时间】:2019-01-06 04:07:53
【问题描述】:

请记住,我对 Python 有点陌生,对 discord 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

【问题讨论】:

    标签: python function discord discord.py


    【解决方案1】:

    您的offtalker() 函数中有错字。你写道:

    global ontalk
    

    但是你尝试修改的全局是on_talk,所以变量只在函数的本地范围内更改为False。而是写:

    global on_talk
    

    【讨论】:

    • 哇,我真的很笨。我想我可能是个盲人。我不明白我是如何被困在这一天的。但显然这不足以解决问题。有什么建议吗?
    • @SKIENCE 你能否让它开始与:TALK 成功交谈?如果是这样,我在您当前的实现中看不到其他错误。我猜这个错误会出现在def talk(ctx)... 的内部,也许它有一个循环正在重置on_talk,或者只是在检查了一次on_talk 的值后忽略了它。没有代码很难说。
    • 那是代码,是的,谈话功能有效。我不明白为什么停止通话功能不会。
    • @SKIENCE 所以talk(ctx)... 函数中没有额外的代码?那你怎么知道它是否已经停止/开始说话了呢?
    • @SKIENCE 还考虑将 print("on_talk is False.") 替换为 print("on_talk is {}".format(on_talk)),这样它就可以打印 on_talk 本身的值。如果某处出现逻辑错误使 on_talk 无法保持False,您将在打印语句中看到。
    猜你喜欢
    • 2021-10-22
    • 1970-01-01
    • 2021-07-18
    • 2022-10-15
    • 2019-10-06
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 1970-01-01
    相关资源
    最近更新 更多