繁体   English   中英

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

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

我正在构建一个票务机器人,每当有人对“票务”类别中的消息做出反应时,它都会创建一个频道,如何获取频道对象以便机器人可以在那里发送消息?

我有这个:

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

同样在打开应该包含票数的 ticket_nums 文件时,它一直给我这个错误:(每次我尝试打开文件时它都会给出)

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'

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

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

然后您可以使用普通的TextChannel.send()方法。

创建频道时,它会返回频道 object。 多亏了这一点,您可以将它放入一个变量中,然后send()它:

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

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

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


参考:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM