2

I'm trying to write a discord bot that checks for the word "pawel" in every message sent and if it finds one, it should send a message to a discord text channel called #pawel. If there isn't one it should create it. Right now I'm stuck with this stupid error:

We're live!
Ignoring exception in on_message
Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "main.py", line 22, in on_message
    channel = discord.utils.get(guild.text_channels, name='pawel')
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/utils.py", line 269, in get
    for elem in iterable:
TypeError: 'property' object is not iterable

The full code for the bot down here:

import discord
import os

client = discord.Client()
guild = discord.Guild

@client.event
async def on_ready():
    print("We're live!")

pawel = ('pawel')

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if pawel in message.content:
        channel = discord.utils.get(guild.text_channels, name='pawel')
        channel.id = channel.id
        if channel == None:
            await guild.create.text_channel('pawel')
        await message.channel.send('pawel')

client.run(os.getenv('TOKEN'))

I've tried everything!

2 Answers 2

3

I think that the issue here is that you're not actually getting an individual guild, but just producing a general Guild Object guild = discord.Guild You can get the guild's text channels by doing message.guild.text_channels

When adapted to your code it would result in:

channel = discord.utils.get(message.guild.text_channels, name="pawel")
Sign up to request clarification or add additional context in comments.

Comments

1
pawel = 'pawel'
@client.event
async def on_message(message):
        if message.author == client.user:
            return
        if pawel in message.content:
            channel = discord.utils.get(message.guild.text_channels, name = 'pawel')
            if channel is None:
                await guild.create_text_channel('pawel')
            await channel.send('pawel')

Kindly remove the channel.id = channel.id (as if the channel is none then channel.id will throw error 'None type object does not have attribute id', and frankly has no use here)

change the guild.text_channel to message.guild_text_channels. Also it's guild.create_text_channel not guild.create.text_channel

I believe you wanted to send the message to the channel named pawel. In such a case, message.channel.send is inappropriate as it will send message to the channel which triggered the event. However, if this wasn't intended then you may leave it. (if it was intended like the way I described, use channel.send instead of that to send it to the channel named "pawel"

The above code was obtained after a few minutes of debugging from my side. Just incase if you are confused about the message.channel.send and channel.send issue, I will leave the modified original code below this line.

Also you can use is for None type objects instead of ==

pawel = 'pawel'
@client.event
async def on_message(message):
        if message.author == client.user:
            return
        if pawel in message.content:
            channel = discord.utils.get(message.guild.text_channels, name = 'pawel')
            if channel is None:
                await guild.create_text_channel('pawel')
            await message.channel.send('pawel')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.