在discord.py中,如何在刚创建的频道上发送消息?

时间:2020-06-11 19:01:48

标签: python discord discord.py

我正在制作票务机器人,每当有人对“票证”类别中的消息作出反应时,它都会创建一个频道,如何获取频道obj,以便该机器人可以在此处发送消息?

我有这个:

@client.event
async def on_raw_reaction_add(payload):
    r_msg_id = payload.message_id
    print(r_msg_id)
    channel_id = payload.channel_id
    user_id = payload.user_id
    channel = client.get_channel(channel_id)
    category_id = int("719621258106372219")
    category = client.get_channel(category_id)
    client.get_channel(r_msg_id)
    msg_obj = await channel.fetch_message(r_msg_id)
    guild_id = payload.guild_id
    guild = client.get_guild(guild_id)
    member = payload.member
    f = open("ticket_nums.txt", "w+")
    #ticket_num = f.read()
    #f.write(int(ticket_num) + 1)
    #f.close()
    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 user_id == "718528256365559829":
            await msg_obj.remove_reaction("\U0001f4e8", discord.Object(id=user_id))
            await category.create_text_channel(r"ticket#" , overwrites=overwrites) #mette i permessi alla persona che apre il ticket

还在打开应该包含故障单计数的ticket_nums文件时,它使我不断出现以下错误:(每次尝试打开文件时都会给出该错误)

Ignoring exception in on_raw_reaction_add
Traceback (most recent call last):
  File "C:\Users\giaco\AppData\Local\Programs\Python\Python37\lib\site-packages\discord\client.py", line 312, in _run_event
    await coro(*args, **kwargs)
  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'

2 个答案:

答案 0 :(得分:1)

要在新创建的频道中发送消息,可以在创建频道时设置一个变量。

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

然后,您可以使用常规的TextChannel.send()方法。

答案 1 :(得分:1)

创建通道时,它将返回通道对象。因此,您可以将其放入变量中,然后send()将其放入

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

至于您的Permission denied错误,我怀疑您需要提升帐户权限。为了使程序具有必要的权限,您可以运行作为管理员使用的任何IDE:

右键单击>以管理员身份运行>允许>运行脚本


参考: