通过Discord.py从TKINTER GUI发送Discord消息
我正在尝试使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如错误所述,您不能两次使用Coroutine。
有一种方法可以解决这个问题。您可以使用自定义
__调用__
创建一个类,该类别在试图调用时会改变行为。当您执行
sender()
时,它将创建 new coroutine并将其添加到客户端循环中。然后,您可以执行类似的操作:
这是一个更新的版本,可以多次调用任何 coroutine。
您可以像
呼叫者(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.When you do
sender()
, it will create a new coroutine and add it to the client loop.You can then do something like this:
Here's an updated version that lets you call any coroutine multiple times.
You can call this like
caller(ctx.send, 'random message', embed=discord.Embed(title='test'))