I am trying to make my bot send an embed DM to a user that join one specific server and send a message saying 'Welcome to Unknown Server
' for any other server. I am trying to do this by getting the bot to get the server id of the server the user joined and compare it between the one I have inputted but, I am getting this error:
C:\Users\mnot7>"C:\PC Code\Python Code Files\Discord Bot\Next cord\bot file name - NextCord.py"
File "C:\PC Code\Python Code Files\Discord Bot\Next cord\bot file name - NextCord.py", line 87
if guild_id == '442806943153651722':
^
IndentationError: unindent does not match any outer indentation level
My code:
@bot.event
async def on_member_join(member):
guild_id = bot.get_guild(guild_id)
channel = bot.get_channel(channel id here)
if guild_id == 'server id here':
await channel.send(f"<@my user id> {member} has joined Server name example!")
await member.send("Welcome to Server name example!")
em = nextcord.Embed(title="Example", description="Example", color=0xFF0000)
em.add_field(name="Example", value="Example", inline=False)
await member.send(embed=em)
else:
await channel.send(f"<@my user id> {member} has joined `Unknown Server`!")
await member.send("Welcome to `Unknown Server`!")
Is there a better way to do this and if not why am I getting the error? I have checked that I only used tab for indentation.
CodePudding user response:
The if
and else
in your code are not indented properly, they need to be on the same level as the rest of the lines under the function—however, the lines you want to execute inside the if
should be indented.
@bot.event
async def on_member_join(member):
guild_id = bot.get_guild(guild_id)
channel = bot.get_channel(channel id here)
if guild_id == 'server id here':
await channel.send(f"<@my user id> {member} has joined Server name example!")
await member.send("Welcome to Server name example!")
em = nextcord.Embed(title="Example", description="Example", color=0xFF0000)
em.add_field(name="Example", value="Example", inline=False)
await member.send(embed=em)
else:
await channel.send(f"<@my user id> {member} has joined `Unknown Server`!")
await member.send("Welcome to `Unknown Server`!")
This should work, make sure to have a consistent indentation standard (e.g same nubmer of spaces, always using tabs).