Discord.py:尝试通过我的机器人发送自定义消息

时间:2021-03-24 14:29:36

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

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

上下文:

对于当前情况,我的机器人将发送我自己发送的自定义消息。为此,我创建了一个 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)

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

谢谢! :)

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

你可以尝试改变:

text.delete(1.0, 'end')

进入:

await text.delete(1.0, 'end')