通过Discord.py从TKINTER GUI发送Discord消息

发布于 2025-02-10 23:36:44 字数 1104 浏览 0 评论 0原文

我正在尝试使用Discord.py库使用其名称将消息发送到频道。 这个库是异步的,我在发送超过1个消息方面存在问题。当我尝试发送一条消息和send函数已经调用时,我会得到这个

Task exception was never retrieved
future: <Task finished name='Task-13' coro=<send() done, defined at C:\Users\paula\DiscordBot\Bot.py:65> exception=RuntimeError('cannot reuse already awaited coroutine')>
RuntimeError: cannot reuse already awaited coroutine

send函数:

async def send():
    global en_message, en_ChannelName
    guild = client.get_guild(some_guild_ID_you_want)
    for channel in guild.channels:
        if en_ChannelName.get().lower() in channel.name.lower() and type(channel) == discord.channel.TextChannel and en_ChannelName.get() != "":
            await channel.send(en_message.get())
            break

它调用的代码:

bt_Send = tk.Button(text="Send", command=partial(client.loop.create_task, send()))

我还必须提及的是,Discord客户端正在运行非序列线 (代码

thread_runBot = t.Thread(target=partial(client.run, botToken))
thread_runBot.start()
TK_dialog.mainloop()

:)

I am trying to use the discord.py library to send messages to a channel using it's name.
This library is asynchronous and I have problems with sending more than 1 message. when I try to send a message afer the send function has already been called, I get this

Task exception was never retrieved
future: <Task finished name='Task-13' coro=<send() done, defined at C:\Users\paula\DiscordBot\Bot.py:65> exception=RuntimeError('cannot reuse already awaited coroutine')>
RuntimeError: cannot reuse already awaited coroutine

the send function :

async def send():
    global en_message, en_ChannelName
    guild = client.get_guild(some_guild_ID_you_want)
    for channel in guild.channels:
        if en_ChannelName.get().lower() in channel.name.lower() and type(channel) == discord.channel.TextChannel and en_ChannelName.get() != "":
            await channel.send(en_message.get())
            break

the code it is called by :

bt_Send = tk.Button(text="Send", command=partial(client.loop.create_task, send()))

I also have to mention that the discord client is running on a non-main thread
(the code :

thread_runBot = t.Thread(target=partial(client.run, botToken))
thread_runBot.start()
TK_dialog.mainloop()

)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绿萝 2025-02-17 23:36:44

如错误所述,您不能两次使用Coroutine。

有一种方法可以解决这个问题。您可以使用自定义__调用__创建一个类,该类别在试图调用时会改变行为。

class CoroutineCaller:
    def __call__(*args, **kwargs):
        # you can also use `asyncio.get_event_loop().create_task` if this doesn't work
        client.loop.create_task(send(*args, **kwargs))

sender = CoroutineCaller()

当您执行sender()时,它将创建 new coroutine并将其添加到客户端循环中。

然后,您可以执行类似的操作:

bt_Send = tk.Button(text="Send", command=sender)

这是一个更新的版本,可以多次调用任何 coroutine。

class CoroutineCaller:
    def __call__(coro, *args, **kwargs):
        client.loop.create_task(coro(*args, **kwargs))

您可以像呼叫者(ctx.send,'Random Message',embed = discord.embed(title ='test'))

As the error states, you can't use a coroutine twice.

There's a way to get around this though. You can create a class with a custom __call__ that changes the behavior when it tries to be called.

class CoroutineCaller:
    def __call__(*args, **kwargs):
        # you can also use `asyncio.get_event_loop().create_task` if this doesn't work
        client.loop.create_task(send(*args, **kwargs))

sender = CoroutineCaller()

When you do sender(), it will create a new coroutine and add it to the client loop.

You can then do something like this:

bt_Send = tk.Button(text="Send", command=sender)

Here's an updated version that lets you call any coroutine multiple times.

class CoroutineCaller:
    def __call__(coro, *args, **kwargs):
        client.loop.create_task(coro(*args, **kwargs))

You can call this like caller(ctx.send, 'random message', embed=discord.Embed(title='test'))

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文