Send a message when the bot joins a server

11,843

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))
Share:
11,843
Spinningjenny
Author by

Spinningjenny

Updated on June 29, 2022

Comments

  • Spinningjenny
    Spinningjenny almost 2 years

    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