【问题标题】:Discord.py: Trying to send custom messages via my botDiscord.py:尝试通过我的机器人发送自定义消息
【发布时间】:2021-05-06 23:30:41
【问题描述】:

最近我一直在制作一个机器人并做了很多,但就在不久前我遇到了这个错误,我并没有真正解决这个错误。所以我想我可以请你帮忙。

上下文:

对于目前的情况,我的机器人是发送一条我自己发送的自定义消息。为此,我创建了一个 tkinter 窗口,其中有一个Text 小部件和一个Button。我在Text 小部件中输入要发送的消息,然后单击Button 发送消息。机器人必须发送该消息,就像 正在发送它一样。

错误:

脚本运行良好,但每当我单击按钮发送文本时。它向我显示了下面提到的错误:

C:\Users\Bhavyadeep\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py:1421: RuntimeWarning: coroutine 'custom_message.<locals>.cm_send' was never awaited
  self.tk.mainloop(n)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

我自己尝试了几件事,但似乎都没有。

代码:

这是与此问题相关的代码:

@NucleoTech.command()
async def custom_message(ctx):
    if ctx.author.id == myid:
        confirm_embed = discord.Embed(title='Custom Message Window Opened!', description='Hey Bhavyadeep! I have opened the custom message window now. You can see it is visible to you now.', color=0x4fff4d)
        confirm_embed.set_footer(text=ctx.author)
        await ctx.channel.send(embed=confirm_embed)
        cm_window = Tk()

        cm_window.geometry('300x300')
        cm_window.resizable(False, False)

        cm_text = Text(cm_window, height=15, width=36)
        cm_text.place(x=2.75, y=1)

        async def cm_send():
            text = cm_text.get(1.0, 'end')
            sendcm_embed = discord.Embed(title='Custom Message!', desciption=f'{text}')
            sendcm_embed.set_author(name='NucleoBot')
            sendcm_embed.set_footer(text=ctx.author)
            await ctx.channel.send(embed=sendcm_embed)
            await asyncio.sleep(2)
            text.delete(1.0, 'end')

        cm_send_button = Button(cm_window, text='Send', command=cm_send, width=20, height=2)
        cm_send_button.place(x=2, y=250)

        cm_window.mainloop()

    if ctx.author.id != myid:
        errorperm_embed = discord.Embed(title='Access Denied!', description='This command is `OWNER` only. You are not allowed to use this. Try not to execute it another time.', color=0xFF0000)
        errorperm_embed.set_footer(text=ctx.author)
        await ctx.channel.send(embed=errorperm_embed)

如果您仍然需要完整的代码,您可以随时向我索取。我会发送它。 :)

谢谢! :)

【问题讨论】:

  • 我会看线程。将 tkinter 保留在主线程中(因为它不是线程安全的)并在另一个线程中运行机器人。
  • @Henry 感谢您的帮助,但我不明白您的意思。你想让我在另一个文件中运行这个窗口,然后用 Bot 的运行代码将它导入到主文件中?
  • Something like this 可能会有所帮助

标签: python python-3.x tkinter discord.py


【解决方案1】:

这更多是 Tkinter 的问题。正如@henry 所说,它不是线程安全的,也就是说,它在与您的机器人不同的处理器核心 中运行Tkinter,并且当机器人尝试时从文本小部件访问文本,它无法获取它。

【讨论】:

    【解决方案2】:

    你可以尝试改变:

    text.delete(1.0, 'end')
    

    进入:

    await text.delete(1.0, 'end')
    

    【讨论】:

    • 此行不能等待。它会显示一个错误。
    猜你喜欢
    • 1970-01-01
    • 2020-10-10
    • 2020-11-04
    • 2021-06-02
    • 1970-01-01
    • 2022-01-03
    • 1970-01-01
    • 2021-10-14
    • 2021-01-26
    相关资源
    最近更新 更多