让成员在modmail功能[discord.py]

时间:2020-09-12 11:19:33

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

我正在尝试在服务器中创建Modmail功能,该功能是什么:

1-用户向bot发送dbot后,就会创建一个modmail类别的频道,该频道名称与为该bot命名的用户ID相同。

2-用户随后发送给漫游器的任何消息都将发送到modmail频道。

3-在modmail频道中发送的所有消息都已发送给用户。

漫游器知道将消息发送给谁的方式是通过频道名称,因为该频道名称等于打开票证的用户的ID,但是,当漫游器试图获取具有该ID的成员时,频道名称,它始终返回None。

这是我的代码:

@bot.event
async def on_message(message):
    guild = bot.get_guild(704414884187602994) ## Get the modmail guild
    category = discord.utils.get(guild.categories, id=754287971347464223) ## Get the modmail category
    overwrites = {
    guild.default_role: discord.PermissionOverwrite(read_messages=False),
    guild.me: discord.PermissionOverwrite(read_messages=True)
} ## Permissions for ticket channel
    
    if isinstance(message.channel, DMChannel): ## Check if a user dms the bot
        if message.author != bot.user: ## Doesn't make the bot respond to itself
            channel = discord.utils.get(guild.channels, name=f"{message.author.id}") ## Ticket channel
            if channel is None: ## Check if there is no open tickets for this user
                await message.channel.send("Welcome to Modmail!") ## Sends message when user dms the bot
                await category.create_text_channel(name=f"{message.author.id}", overwrites=overwrites) ## Create ticket channel if none found
            elif channel is not None: ## If ticket found
                await channel.send(f"Message recieved from {message.author.name}#{message.author.discriminator}, content: {message.content}") ## Send message to ticket channel
    if message.channel.category == category: ## If a message is sent to the ticket channel
        TheID = message.channel.name ## The ticket channel name which is the same name as the reporter ID
        reporter = guild.get_member(TheID) ## Get the reporter to send messages to
        print(TheID) ## Print the ID of the reporter
        if reporter is None: ## If no user has the ID given in the channel name
            print("Member not found.")
        else:   
            await reporter.send(f"Message recieved from bot, content = {message.content}") ## Send message to reporter

有关此问题的任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

问题出在TheID = message.channel.name这一行

TextChannel.name返回一个字符串,该字符串是通道的名称,并且get_member()必需的ID应该是Integer,因此您应该进行reporter = guild.get_member(int(TheID))

答案 1 :(得分:0)

        TheID = message.channel.name 
        reporter = guild.get_member(int(TheID)) 
        print(TheID) 
        if reporter is None: 
            print("Member not found.")
        else:   
            await reporter.send(f"Message recieved from bot, content = {message.content}")```