brandone
brandone

Reputation: 74

welcome/goodbye using discord.py

I'm trying to create a bot that greets a user that joins a server. But make it so that the person is greeted in the server itself rather than as a DM (which most tutorials I've found teach you how to do).

This is what I've come up with so far.

@bot.event
async def on_member_join(member):
    channel = bot.get_channel("channel id")
    await bot.send_message(channel,"welcome") 

but, it doesn't work and instead throws up this error.

Ignoring exception in on_member_join
Traceback (most recent call last):
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python36\lib\site- 
packages\discord\client.py", line 307, in _run_event
yield from getattr(self, event)(*args, **kwargs)
File "C:\Users\Lenovo\Documents\first bot\bot.py", line 26, in 
on_member_join
await bot.send_message(channel,"welcome")
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python36\lib\site- 
packages\discord\client.py", line 1145, in send_message
channel_id, guild_id = yield from self._resolve_destination(destination)
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python36\lib\site- 
packages\discord\client.py", line 289, in _resolve_destination
raise InvalidArgument(fmt.format(destination))
discord.errors.InvalidArgument: Destination must be Channel, PrivateChannel, 
User, or Object. Received NoneType

Upvotes: 1

Views: 4281

Answers (3)

bobby
bobby

Reputation: 1

Try this:

@bot.event
async def on_member_join(member):
    channel = discord.utils.get(member.guild.channels, name='the name of the channel')
    await channel.send(f'Enjoy your stay {member.mention}!')

Upvotes: 0

Onfe
Onfe

Reputation: 26

The answer by Patrick Haugh is probably your best bet, but here are a few things to keep in mind.

The Member object contains the guild (server), and the text channels the server contains. By using member.guild.text_channels you can ensure the channel will exist, even if the server does not have a 'general' chat.

@bot.event
async def on_member_join(member):
    channel = member.guild.text_channels[0]
    await channel.send('Welcome!')

Upvotes: 0

Patrick Haugh
Patrick Haugh

Reputation: 60944

You aren't passing the correct id to get_channel, so it's returning None. A quick way to get it would be to call the command

from discord.ext import commands

bot = commands.Bot(command_prefix='!')

@bot.command(pass_context=True)
async def get_id(ctx):
    await bot.say("Channel id: {}".format(ctx.message.channel.id))

bot.run("TOKEN")

You could also modify your command to always post in a channel with a particular name on the server that the Member joined

from discord.utils import get

@bot.event
async def on_member_join(member):
    channel = get(member.server.channels, name="general")
    await bot.send_message(channel,"welcome") 

Upvotes: 1

Related Questions