SDG8
SDG8

Reputation: 79

how to make discord py bot send a message when it is invited to a server

I have a discord.py bot and I use this script to make the bot send messages to main channel when it gets invited to a server! However this doesn't send anything to the server! Please Help!

@client.event
async def on_guild_join(guild):
    print("NEW SERVER WAS JOINED!")
    general = find(lambda x: x.name == 'general',  guild.text_channels)
    if general and general.permissions_for(guild.me).send_messages:  
        await general.send(f"""
████████╗░░░░░░██████╗░░█████╗░████████╗
╚══██╔══╝░░░░░░██╔══██╗██╔══██╗╚══██╔══╝
░░░██║░░░█████╗██████╦╝██║░░██║░░░██║░░░
░░░██║░░░╚════╝██╔══██╗██║░░██║░░░██║░░░
░░░██║░░░░░░░░░██████╦╝╚█████╔╝░░░██║░░░
░░░╚═╝░░░░░░░░░╚═════╝░░╚════╝░░░░╚═╝░░░
```Thanks For Inviting the bot!
Made with <3 by TOG6#6666 with Tons of help from the TOG6 community!```
***Server Invite:*** https://discord.gg/vSxuAbq""")
        await general.send(f"``` Type $help to view all commands!```")

Upvotes: 0

Views: 3723

Answers (4)

Thunder
Thunder

Reputation: 501

I’m better at discord.js, but I’ll give your discord.py problem a stab…

@client.event
async def on_guild_join(guild):
    try:
        joinchannel = guild.system_channel
        # The system channel is where Discord’s join messages are sent
        await joinchannel.send('Thanks for inviting me to your server!')
    catch:
        # if no system channel is found send to the first channel in guild
        await guild.text_channels[0].send(<message>)

Upvotes: 1

Nicholas Chen
Nicholas Chen

Reputation: 144

@client.event
async def on_guild_join(guild):
    for channel in guild.text_channels:
        if channel.permissions_for(guild.me).send_messages:
            await channel.send('I will send this message when I join a server')
        break

Upvotes: 1

SDG8
SDG8

Reputation: 79

I last time made an answer saying that you could use the on_message, but then I came up with something 2,000 times better! Each server has a system channel and using your discord.py bot, you can find the system channel and make it send it's message there!

That's the best method to do it as most server owners leave a main or important channel as their system channel!

Upvotes: 0

SDG8
SDG8

Reputation: 79

I wanted to thank everyone for replying to this post! However I came up with a logic!

Since my bot takes admin permissions, it can post on any channel!

Now the thing is, the bot posts the main message whenever someone says T-BOT(case sensitive)

That way, no one is ever gonna say the bot in its entirety at the same time, the bot posts its first message!

For those wondering how to do it, you can use the on_message discord event to make it search for the bots name!

I hope I helped someone in the future who is making a discord.py bot! :-)

Upvotes: 2

Related Questions