TNSabi
TNSabi

Reputation: 43

Error when sending an message to specified channel using discord.py

I ran into an problem while trying to send a message to a specified channel when someone joins my discord. I got this Error:

Ignoring exception in on_member_join
Traceback (most recent call last):
  File "C:\Users\Timo\Anaconda3\envs\Discord\lib\site-packages\discord\client.py", line 270, in _run_event
    await coro(*args, **kwargs)
  File "C:/Users/Timo/PycharmProjects/Discord/Bot 1/Main.py", line 30, in on_member_join
    channel = Bot.get_channel("649275309396590626")
TypeError: get_channel() missing 1 required positional argument: 'id'

I'm using version v1.2.5. Here's the code:

@client.event
async def on_member_join(member):
    channel = Bot.get_channel("649275309396590626")
    await channel.send(f"{member} ist dem Server beigetreten!")

Here's the Bot Variable:

import discord
from discord.ext import commands
from discord.ext.commands import Bot

client = Bot(command_prefix=".")

I edited it to:

@client.event async def on_member_join(member): channel = client.get_channel("649275309396590626") await channel.send(f"{member} ist dem Server beigetreten!")

Now i have this Error:

File "C:/Users/Timo/PycharmProjects/Discord/Bot 1/Main.py", line 31, in on_member_join await channel.send(f"{member} ist dem Server beigetreten!") AttributeError: 'NoneType' object has no attribute 'send'

Upvotes: 2

Views: 155

Answers (2)

Deru
Deru

Reputation: 1173

The client.get_guild(id) function requires an integer input. When this is not correctly given or the id is invalid the function will return a None value. When you try to send something using the NoneType object you cause an error. As a NoneType object doesnt have a "send' function.

I recommend reading the documentation: https://discordpy.readthedocs.io/en/latest/api.html?highlight=get_guild#discord.Client.get_guild

Upvotes: 1

TNSabi
TNSabi

Reputation: 43

I found the problem, it is that i set an string as ID instead of an int. Now the code looks like this:

@client.event
async def on_member_join(member):
    channel = client.get_channel(649275309396590626)
    await channel.send(f"{member} ist dem Server beigetreten!")

Upvotes: 0

Related Questions