3
votes

I want to send a message everytime when the bot gets invited to a server. It should then write something like: "Hello this is my discord bot"

So far I have this code, which produces no errors, but also doesn't send the message.

@bot.event
async def on_server_join(ctx):
    for guild in bot.guilds:
        for channel in guild.text_channels:
            if channel.permissions_for(guild.me).say:
                await ctx.message.channel.send('Hello! \n')
                break
1
If you're on the rewrite branch, the event has been renamed to on_guild_join, plus there is not context. The only argument should be a Guild object representing the server you have joined.Patrick Haugh

1 Answers

6
votes

There are a couple of errors in your code. Below is a version that just says hello in the #general text channel of the server it just joined (as opposed to every text channel of every server it is a member of). The below code is for the rewrite branch.

from discord.utils import find

@client.event
async def on_guild_join(guild):
    general = find(lambda x: x.name == 'general',  guild.text_channels)
    if general and general.permissions_for(guild.me).send_messages:
        await general.send('Hello {}!'.format(guild.name))