James
James

Reputation: 168

'TextChannel' object is not iterable | channel check not working

Hi I'm trying to check to see if a channel already exists instead of creating a new one each time a member sends a modmail request. The error I get is

File "C:\Users\User\Desktop\Build 1.0.2\cogs\modmail.py", line 145, in on_message
if get(modmailchannel, name=f"{message.author.name.lower()}{message.author.discriminator}"):
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\discord\utils.py", line 271, in get
for elem in iterable:
TypeError: 'TextChannel' object is not iterable
enter code here

not sure why or the method is incorrect some how. The channel name that is created is the users name and discriminator. What I'm trying to achieve is if that channel already exists don't create another channel/post in the same channel.

Help appreicated

Here is the code I'm working with:

@commands.Cog.listener()
async def on_message(self, message):
    if not isinstance(message.channel, discord.DMChannel) or message.author.id == self.bot.user.id:
        # not a DM, or it's just the bot itself
        return
    
    bucket = self._cd.get_bucket(message)
    retry_after = bucket.update_rate_limit()
    seconds = bucket.update_rate_limit()
    seconds = round(seconds, 2)
    hours, remainder = divmod(int(seconds), 3600)
    minutes, seconds = divmod(remainder, 60)
    if retry_after:
        pass
    else:
        channel = self.bot.get_channel(744311308509249610)
        if not channel:
            print("Mail channel not found! Reconfigure bot!")
        time = datetime.utcnow()
        guild = self.bot.get_guild(715288565877309521)
        member_role = get(guild.roles, name='Members')
        mod_role = get(guild.roles, name='Members')
        muted_role = get(guild.roles, name='Modmail Muted')
        user_id = message.author.id
        author = guild.get_member(user_id)
        
        content = message.clean_content
        
        if muted_role in author.roles:
            await message.channel.send("You're not allowed to use modmail.")
            return
        
        if len(message.content) < 0:
            await message.channel.send("Your message should be atleast 50 characters in length.")
            return
    
        if member_role in author.roles:
            emoji1 = '📨'
            emoji2 = '🗑️'
            message_confirm = "You're about to send a message to the mod team, react with :incoming_envelope: to confirm otherwise :wastebasket: to discard your message."
            embed = discord.Embed(description=message_confirm, colour=0x5539cc)
            embed.title = "Confirm Message"
            confirm = await message.channel.send(embed=embed)
            await confirm.add_reaction(emoji1)
            await confirm.add_reaction(emoji2)
            check = reaction_check(message=confirm, author=message.author, emoji=(emoji1, emoji2))
            try:
                reaction, user = await self.bot.wait_for('reaction_add', timeout=60.0, check=check)
                if reaction.emoji == emoji1:
                    embed = discord.Embed(title=f"Message Sent", description=f"Hello {message.author.display_name}! We have received your message and we will get back to you as soon as possible. You can send another message in **{minutes}m {seconds}s**.", timestamp=time, colour=0x5539cc)
                    await message.channel.send(embed=embed)
                elif reaction.emoji == emoji2:
                    embed = discord.Embed(title=f"Message Not Sent", description=f"Your message has not been sent. You can send another message in **{minutes}m {seconds}s**.", timestamp=time, colour=0x5539cc)
                    await message.channel.send(embed=embed)
                    return
            except asyncio.TimeoutError:
                await message.channel.send(f'Sorry, you took too long to confirm your message. Try again in **{minutes}m {seconds}s**.')
                return
            overwrites = {
                guild.default_role: discord.PermissionOverwrite(read_messages=False),
                guild.me: discord.PermissionOverwrite(read_messages=True),
                member_role: discord.PermissionOverwrite(read_messages=False),
                mod_role: discord.PermissionOverwrite(read_messages=True)}
            
            embed = discord.Embed(title=f"Modmail message request from — {message.author.name}#{message.author.discriminator}", colour=0xff8100)
            embed.add_field(name="Member:", value=f"{message.author.mention}" ,inline=True)
            embed.add_field(name="Reply ID:", value=f"{message.author.id}" ,inline=True)
            embed.add_field(name="Message:", value=content[:1000] or "blank", inline=False)
            embed.set_footer(text=f"C&P Command: !reply {message.author.id}")
            if message.attachments:
                embed.add_field(name="Attachments", value=", ".join([i.url for i in message.attachments]))
            if len(content[1000:]) > 0:
                embed.add_field(name="Message (continued):", value=content[1000:])
            modmailchannel = await guild.create_text_channel(f'{message.author.name}{message.author.discriminator}', overwrites=overwrites, category=self.bot.get_channel(744944688271720518))
            await modmailchannel.send(embed=embed)
            
            if get(modmailchannel, name=f"{message.author.name.lower()}{message.author.discriminator}"):
                 await modmailchannel.send(embed=embed)
            
        else:
            await message.channel.send("Only members can use modmail.")

Upvotes: 0

Views: 1059

Answers (1)

kantuni
kantuni

Reputation: 442

Instead of if get(modmailchannel, name=f"{message.author.name.lower()}{message.author.discriminator}"): use:

if modmailchannel.name == f"{message.author.name.lower()}{message.author.discriminator}":

Upvotes: 1

Related Questions