Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Discord bot项目中运行额外的Python代码_Python_Discord_Bots_Python Asyncio - Fatal编程技术网

如何在Discord bot项目中运行额外的Python代码

如何在Discord bot项目中运行额外的Python代码,python,discord,bots,python-asyncio,Python,Discord,Bots,Python Asyncio,我正在尝试运行我的discord bot,退出()或注销(),运行一些其他python代码,然后重新登录。我没有使用异步函数的经验,所以我不知道错误消息在说什么 import discord dToken = "xxxxx" client = discord.Client() @client.event async def on_ready(): print('Logged in as') print(client.user.name) prin

我正在尝试运行我的discord bot,退出()或注销(),运行一些其他python代码,然后重新登录。我没有使用异步函数的经验,所以我不知道错误消息在说什么

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)
我必须注销(),因为这是我找到的唯一可以运行额外代码的方法。但我的代码给出了以下错误:

    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
回溯(最近一次呼叫最后一次):
文件“C:/Users/mmh/PycharmProjects/pythonProject/test.py”,第21行,在
client.run(dToken)
文件“C:\Users\mmh\PycharmProjects\pythonProject\venv\lib\site packages\discord\client.py”,第665行,正在运行
future=asyncio。确保未来(runner(),loop=loop)
文件“C:\Users\mmh\AppData\Local\Programs\Python\Python37\lib\asyncio\tasks.py”,第608行,以确保将来
任务=循环。创建任务(coro或未来)
文件“C:\Users\mmh\AppData\Local\Programs\Python37\lib\asyncio\base\u events.py”,第402行,位于创建\u任务中
自我检查关闭()
文件“C:\Users\mmh\AppData\Local\Programs\Python37\lib\asyncio\base\u events.py”,第479行,在\u check\u closed中
raise RUNTIMERROR('事件循环已关闭')
RuntimeError:事件循环已关闭
sys:1:RuntimeWarning:coroutine“Client.run..runner”从未被等待过
进程已完成,退出代码为1

有人知道我应该做什么吗?

据我所知,你不能运行
客户端。连续运行
两次(我自己尝试过,得到了相同的
运行时错误
)。但是,在本例中,您似乎希望向两个不同服务器中的两个通道发送消息。如果是这样,您只需在_ready
上的
内创建一个函数,向给定频道发送消息,然后您可以调用该函数两次:

@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!')
await client.run(dToken)
?@JacobIRR“await”仅在def on_ready()函数中起作用