1
votes

I'm a newbie in discord.py and want to understand some features. How can I execute function without literally running the bot? You see, I'd like to call some bot function from other context. I don't like my bot to read events or smth like that, just to, for example, create a voice channel whenever my program call for that. How can I make such function? Maybe I should look at writing custom handlers for discord API with, for example, requests lib?

1

1 Answers

1
votes

Your bot must logged into Discord to do things with the api.

I recomend you to run the bot in a diffrent thread and run the coro from the main tread. Like this:

This is your file where your bot is in:

from discord.ext.commands import Bot
import threading
import discord

client = Bot(command_prefix="!")



@client.event
async def on_ready():
    print("Ready")


async def create_voice(guild_id):
    await client.wait_until_ready() #  Waits for bot is logged in and the cache is loaded
    guild = client.get_guild(guild_id)
    await guild.create_voice_channel("Hallo")
    print("Done")



thread = threading.Thread(target=client.run, args=["Token"])
thread.start()

main file:

import bot_run
import asyncio

loop = asyncio.get_event_loop()

asyncio.run_coroutine_threadsafe(bot_run.create_voice(guild_id), loop)

Hope this helps :D