James
James

Reputation: 3

Discord bot won't run startup sequence

I am making a discord bot and it won't run a sequence that I made that says when it joins a server, it should make a role, a category, and two channels in that category.

async def on_server_join(ctx):
  await ctx.guild.create_category('Friend')
  await ctx.guild.create_role(name="friend.admin", mentionable=True)
  category = ctx.guild.utils.get(ctx.guild.categories, name='Friend')
  await ctx.guild.create_text_channel('Chat-with-@Friend', category=category)
  guild = ctx.guild
  admin_role = ctx.guild.roles, name="Admin"
  overwrites = {
        guild.default_role: ctx.guild.PermissionOverwrite(read_messages=False),
        guild.me: ctx.guild.PermissionOverwrite(read_messages=True),
        admin_role: ctx.guild.PermissionOverwrite(read_messages=True)
    }
  await guild.create_text_channel('secret', overwrites=overwrites, category=category)```

Upvotes: 0

Views: 170

Answers (2)

Ceres
Ceres

Reputation: 2658

To control what happens when your bot joins a server, you will need to use the on_guild_join event. You are trying to use on_server_join with a ctx parameter, ctx is generally used as the context for commands.

@bot.command()
async def on_guild_join(guild):  #guild is a discord.Guild
    await guild.create_channel('Friend')
    # make sure you have permissions

References:

Upvotes: 0

Aditya Tomar
Aditya Tomar

Reputation: 1639

This is because the event is on_guild_join(guild). Try this out:

@bot.event # or client.event based on your code
async def on_guild_join(guild):
    await guild.create_category('Friend')
    # rest of your code

So, instead of passing in ctx, the event takes in guild, and you can create channels, roles, categories, etc., with guild.create instead of ctx.guild.create.

Upvotes: 1

Related Questions