Reputation: 145
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
Upvotes: 2
Views: 14988
Reputation: 60974
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))
Upvotes: 6