Python Discord 机器人在交给功能时不会说什么
Python Discord bot won't say something when handed to function
我刚开始修改 Python 响应 Discord 查询的程序。
运行 该程序会启动一个 Discord 机器人,该机器人会侦听命令并相应地执行任务。现在我想将 bot 对象的引用提供给方法,它开始将一些信息返回给 discord 频道,了解现在正在发生的事情。出于某种原因,这不起作用,我不知道为什么。
这是最小示例。如果缺少什么,请告诉我。可以查看完整代码 here.
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 的机器人和来自 playgame.py 的 self.discordbot 似乎引用了同一个对象。在 运行 之后,这个不和谐说
Game started
但他不会说
did something
有什么问题可以让我调查吗?
提前致谢。
bot.say
是 coroutine,因此您必须 yield from
或 await
结果。
另外,你cannot use bot.say
outside of commands
您应该尝试将游戏 运行 的所有代码与控制机器人的代码分开。
我刚开始修改 Python 响应 Discord 查询的程序。
运行 该程序会启动一个 Discord 机器人,该机器人会侦听命令并相应地执行任务。现在我想将 bot 对象的引用提供给方法,它开始将一些信息返回给 discord 频道,了解现在正在发生的事情。出于某种原因,这不起作用,我不知道为什么。
这是最小示例。如果缺少什么,请告诉我。可以查看完整代码 here.
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 的机器人和来自 playgame.py 的 self.discordbot 似乎引用了同一个对象。在 运行 之后,这个不和谐说
Game started
但他不会说
did something
有什么问题可以让我调查吗? 提前致谢。
bot.say
是 coroutine,因此您必须 yield from
或 await
结果。
另外,你cannot use bot.say
outside of commands
您应该尝试将游戏 运行 的所有代码与控制机器人的代码分开。