Bhavyadeep Yadav
Bhavyadeep Yadav

Reputation: 831

Discord.py: Trying to send custom messages via my bot

Recently I have been into making of a bot and have done a lot but just a while ago I got into this error which is not really getting fixed by me. So I thought I could ask you for some help.

Context:

For the current situation my bot is to send a custom message which I send my self. For this I have created a tkinter window in which I have a Text widget and a Button. I enter the message I want to send inside the Text widget and then I click on the Button to send the message. The bot has to send that message as if it is sending it.

Error:

The script works just fine but whenever I click the button to send the text. It shows me the error mentioned below:

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

I tried several things myself but none seemed to work.

Code:

Here is the code which relates to this issue:

@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)

If you still need the whole code you can ask me for it anytime. I would send it. :)

Thank You! :)

Upvotes: 0

Views: 503

Answers (2)

INdIE DeV
INdIE DeV

Reputation: 36

This is more of Tkinter's problem. As @henry said it's not thread safe, that is, it runs Tkinter in a different processor core than your bot, and when the Bot tries to access the Text from the Text Widget, it is not able to get it.

Upvotes: 2

dharmey
dharmey

Reputation: 85

You could try to change:

text.delete(1.0, 'end')

Into:

await text.delete(1.0, 'end')

Upvotes: 0

Related Questions