How to run extra Python code in a Discord bot project

Viewed 253

I'm trying to run my discord bot, exit() or logout() from it, run some other python code, and then log back in. I don't have experience with async functions, so I have no idea what the error messages is saying.

import discord

dToken = "xxxxx"
client = discord.Client()


@client.event
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)

    guilds = client.guilds
    for i in range(len(guilds)):

        if str(guilds[i]) == server:
           gld = guilds[i]
           for channel in gld.text_channels:
                if str(channel) == "channelname":
                    print(str(message)+" sent to channel id "+str(channel.id))
                    await client.get_channel(channel.id).send(message)

    await client.logout()

#run bot
client.run(dToken)

#change message and server name depending on extra code I put here
message = "hello!"
server = "servername"

#run bot again with changes
client.run(dToken)

I have to logout() because that's the only way I found that can make the extra code run. But the code I have gives the following error:

    Traceback (most recent call last):
  File "C:/Users/mmh/PycharmProjects/pythonProject/test.py", line 21, in <module>
    client.run(dToken)
  File "C:\Users\mmh\PycharmProjects\pythonProject\venv\lib\site-packages\discord\client.py", line 665, in run
    future = asyncio.ensure_future(runner(), loop=loop)
  File "C:\Users\mmh\AppData\Local\Programs\Python\Python37\lib\asyncio\tasks.py", line 608, in ensure_future
    task = loop.create_task(coro_or_future)
  File "C:\Users\mmh\AppData\Local\Programs\Python\Python37\lib\asyncio\base_events.py", line 402, in create_task
    self._check_closed()
  File "C:\Users\mmh\AppData\Local\Programs\Python\Python37\lib\asyncio\base_events.py", line 479, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
sys:1: RuntimeWarning: coroutine 'Client.run.<locals>.runner' was never awaited

Process finished with exit code 1

Does anyone know what I should be doing?

1 Answers

As far as I know, you can't run client.run twice in a row (I tried myself, got the same RuntimeError). However in this case, it looks like you want to send a message to two channels in two different servers. If so, you can simply create a function inside on_ready to send a message to a given channel and then you can call that function twice:

@client.event
async def on_ready():
    async def send_message(server_name, channel_name, message):
        # Instead of looping through indices for client.guilds,
        # you should iterate over client.guilds itself
        # (as you did for gld.text_channels)
        for guild in client.guilds:
            if guild.name != server_name:
                continue

            for channel in guild.text_channels:
                if channel.name == channel_name:
                    await channel.send(message)
                    print(message, 'sent to channel id', channel.id)
                    return
            else:
                raise ValueError('could not find channel')

        else:
            raise ValueError('could not find server')

    await send_message('server1', 'textchannel1', 'hello!')
    await send_message('server2', 'textchannel2', 'hello!')
Related