Skip to content

Instantly share code, notes, and snippets.

@0xicl33n
Last active November 23, 2023 19:02
Show Gist options
  • Save 0xicl33n/e5008c5865347aafc644a67455507314 to your computer and use it in GitHub Desktop.
Save 0xicl33n/e5008c5865347aafc644a67455507314 to your computer and use it in GitHub Desktop.
discord.py rewrite send message when bot joins server
@bot.event
async def on_guild_join(guild):
for channel in guild.text_channels:
if channel.permissions_for(guild.me).send_messages:
await channel.send('Hey there! this is the message i send when i join a server')
break
@LucasBirkemose
Copy link

Wow, thank you I really needed this!

@Modiens
Copy link

Modiens commented May 28, 2020

THX!!

@dicamarques14
Copy link

Thanks

Copy link

ghost commented Sep 19, 2020

Thank you very much

@befreiunn
Copy link

Tank you very mouch THX!!!!

@ealyyyy
Copy link

ealyyyy commented Nov 29, 2020

Yeah this works amazing, but it sends a message to every channel, can I fix this somehow?

@0xicl33n
Copy link
Author

Yeah this works amazing, but it sends a message to every channel, can I fix this somehow?

looks like you forgot to add the break

@greendoescode
Copy link

so i've changed the code to work with my bot but it errors
Ignoring exception in on_guild_join Traceback (most recent call last): File "D:\The_f\python\lib\site-packages\discord\client.py", line 333, in _run_event await coro(*args, **kwargs) TypeError: on_guild_join() takes 1 positional argument but 2 were given
the code that i used was
`import discord
from discord.ext import commands

class role(commands.Cog):

def __init__(self, client):
    self.client = client

@commands.Cog.listener()
async def on_guild_join(guild):
    for channel in guild.text_channels:
        if channel.permissions_for(guild.me).send_messages:
            await channel.send(f'Hey there! do `{prefix}setup` to make your moderation role')
        break

def setup(client):
client.add_cog(role(client))`
any ideas on how to fix it?

you need "self" before guild!

@fuh-Q
Copy link

fuh-Q commented Jan 23, 2021

one thing I noticed about my bot's behaviour is that when it joins, if it can't send messages in the VERY TOP CHANNEL it just doesn't do anything else. Is there a workaround for this?

@0xicl33n
Copy link
Author

one thing I noticed about my bot's behaviour is that when it joins, if it can't send messages in the VERY TOP CHANNEL it just doesn't do anything else. Is there a workaround for this?

I've been using this for three years and it has always posted on the first channel it is able to regardless of its order in the list. No idea what your problem is and this gist isn't for bot support.

@akionsight
Copy link

Wow, this works flawlessly, thanks for helping

@Codex880
Copy link

Thanks bro!

@W1L7dev
Copy link

W1L7dev commented Jul 12, 2021

so i've changed the code to work with my bot but it errors
Ignoring exception in on_guild_join Traceback (most recent call last): File "D:\The_f\python\lib\site-packages\discord\client.py", line 333, in _run_event await coro(*args, **kwargs) TypeError: on_guild_join() takes 1 positional argument but 2 were given
the code that i used was
`import discord
from discord.ext import commands

class role(commands.Cog):

def __init__(self, client):
    self.client = client

@commands.Cog.listener()
async def on_guild_join(guild):
    for channel in guild.text_channels:
        if channel.permissions_for(guild.me).send_messages:
            await channel.send(f'Hey there! do `{prefix}setup` to make your moderation role')
        break

def setup(client):
client.add_cog(role(client))`
any ideas on how to fix it?

Just add "self" before guild at line 9

@akionsight
Copy link

i doesnt work for me, i dont get even a error message.....
i'Ve enabled intents.guilds = True

maybe cuz the library (discord.py) is now dead

@Leksa1234
Copy link

i doesnt work for me, i dont get even a error message.....
i'Ve enabled intents.guilds = True

maybe cuz the library (discord.py) is now dead

hmmmm me is work how you not work?

@Parad1se-py
Copy link

Yes, possible. Try the new library "Py-Cord" and go through their docs, it's pretty similar to DPY but Py-Cord is what I recommend to use now, after all, discord.py is discontinued.

@akionsight
Copy link

@0xicl33n the thing is that, this gist is just super super useful

@BitcoinJake09
Copy link

@0xicl33n the thing is that, this gist is just super super useful

i agree, im using discord.py still and was trying to figure this out exactly lmao, very helpful indeed <3

@YbicG
Copy link

YbicG commented Sep 21, 2022

@0xicl33n Bro you commented though?

@Thecode764
Copy link

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment