Python Discord 机器人在交给函数时不会说些什么



我刚开始修改一个python程序,该程序响应不和谐中的查询。

运行该程序启动一个Discord Bot,该机器人会倾听命令并相应地执行任务。现在,我想将对bot对象的引用引用到开始的方法中,以将一些信息还给目前正在发生的情况。由于某种原因,这是不起作用的,我不知道为什么。

这是最小的例子。如果缺少什么,请告诉我。可以在此处查看完整代码。

discord.py

from communitybot.playgame import Game
from discord.ext.commands import Bot
from communitybot.utils import (
    start_game
)
bot = Bot(
    description=description,
    command_prefix="$",
    pm_help=False)
bot.remove_command('help')
@bot.group(pass_context=True)
async def game(ctx):
    if ctx.invoked_subcommand is None:
        await bot.say('Usage: $game start')
@game.command()
async def start():
    start_game(bot)
    await bot.say("**Game started**")

utils.py

from communitybot.playgame import Game
from communitybot.settings import BOT_ACCOUNT
from steem import Steem
def start_game(discordbot=None):
    c = Game(
        get_steem_conn(),
        BOT_ACCOUNT,
        discordbot = discordbot
    )
    c.start_game()

playgame.py

class Game:
    def __init__(self, steemd_instance, bot_account, discordbot=None):
        self.s = steemd_instance
        self.bot_account = bot_account
        self.discord = discordbot
    def start_game(self):
        #do something
        if self.discord is not None:
            self.discord.say('**did something**')

来自discord.py和self.discordbot的bot。在运行此不和谐之后

游戏开始

,但他不会说

做了一些

关于什么问题的任何建议,所以我可以研究它吗?预先感谢。

bot.say是coroutine,因此您必须yield fromawait结果。

此外,您不能在命令外使用bot.say

您应该尝试将游戏运行的所有代码与控制机器人的代码分开。

最新更新