Tom
Tom

Reputation: 188

Python Discord bot won't say something when handed to function

I just started modifying a Python Program that responds to queries in Discord.

Running the Program starts a Discord bot that listens to commands and executes tasks accordingly. Now I wanted to give a reference to the bot object into the method it started to give some information back to the discord channel on what is happening right now. For some reason that's not working and I can't figure out why.

Here's the min example. If something is missing please tell me. Full code can be reviewed 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**')

The bot from discord.py and self.discordbot from playgame.py seem to be referenced to the same object. After running this discord says

Game started

but he won't say

did something

Any suggestion on what is wrong so I can look into it? Thanks in advance.

Upvotes: 0

Views: 694

Answers (1)

Patrick Haugh
Patrick Haugh

Reputation: 60944

bot.say is a coroutine, so you must either yield from or await the result.

Additionally, you cannot use bot.say outside of commands

You should try to separate all of the code for the running of your game from the code that controls the bot.

Upvotes: 1

Related Questions