user13690062
user13690062

Reputation:

discord.py - How do i send a message on a just created channel?

I am building a ticket bot, it creates a channel every time someone reacts a message in the "ticket" category, how to get the channel obj so the bot can send a message there?

I have this:

@client.event
async def on_raw_reaction_add(payload):
    channel = client.get_channel(payload.channel_id)
    client.get_channel(payload.message_id)
    msg = await channel.fetch_message(payload.message_id)
    guild = client.get_guild(payload.guild_id)
    f = open("ticket_nums.txt", "w+")
    overwrites = {
        guild.default_role: discord.PermissionOverwrite(read_messages=False),
        payload.member: discord.PermissionOverwrite(read_messages=True)
    }
    if True: #r_msg_id == "720708414107287724": commented out because it doesn't work idk why
        if not payload.user_id == "718528256365559829":
            await msg.remove_reaction("\U0001f4e8", discord.Object(id=user_id))
            await client.get_channel(719621258106372219).create_text_channel(r"ticket#" , overwrites=overwrites) 

also while opening the ticket_nums file, which should contain the ticket count, it keeps me giving me this error: (It's giving it every time I try to open a file)

Traceback (most recent call last):
  File "C:\Users\giaco\Desktop\Bot\Discord Bot\bot_discord.py", line 216, in on_raw_reaction_add
    f = open("ticket_nums.txt", "w+")
PermissionError: [Errno 13] Permission denied: 'ticket_nums.txt'

Upvotes: 0

Views: 1207

Answers (2)

Diggy.
Diggy.

Reputation: 6944

When creating a channel, it returns the channel object back. Thanks to this, you can put it into a variable and then send() it:

channel = await category.create_text_channel(...)
await channel.send("Hey! I just created this channel!")

As for your Permission denied error, I suspect you need elevated permissions on your account. To let the program have the necessary permissions, you can run whatever IDE you're using as administrator:

Right-click > Run as administrator > Allow > Run your script


References:

Upvotes: 0

duckboycool
duckboycool

Reputation: 2455

For the sending a message in a newly created channel, you can set a variable when you create the channel.

ticket_channel = await category.create_text_channel(r"ticket#" , overwrites=overwrites)

Then you could use the normal TextChannel.send() method.

Upvotes: 1

Related Questions