Itachi Uchiha
Itachi Uchiha

Reputation: 37

Add ctx in on_member_join Event

So im fairly new to this and this is my first time asking question.

I want my bot to create a new channel when a member joins and make it so only that specific member can view it. Im planning on adding more to it so that it deletes after specific time. I've made it so that it creates a channl but to work with channel permissions I need to have ctx which i cannot define in on_member_join function.

Attaching the code for reference, and yes I'm working with cogs.

@commands.Cog.listener()
  async def on_member_join(self,member : discord.Member):
    ChannelName = member.name
    guild = member.guild
    
    await guild.create_text_channel(name = '{}'.format(ChannelName))
    overwrites = ctx.channel.overwrites_for(ctx.default_role)
    overwrites.read_messages, overwrites.send_messages = False, False
    await ctx.channel.set_permissions(guild.default_role, overwrite=overwrites)
    overwrites = ctx.channel.overwrites_for(ctx.member)
    overwrites.send_messages, overwrites.read_messages = True, True
    await guild.channel.set_permissions(ctx.member, overwrite=overwrites)
    await ctx.send ('Success!!')

Upvotes: 0

Views: 339

Answers (1)

Axisnix
Axisnix

Reputation: 2907

create_text_channel returns the TextChannel which you can use to replace ctx.channel as ctx.channel is the channel that the command was called from. For the default_role it exists from Member which can be accessed from member.guild.default_role

The code below should work:

@commands.Cog.listener()
  async def on_member_join(self,member : discord.Member):
    ChannelName = member.name
    guild = member.guild
    
    channel = await guild.create_text_channel(name = '{}'.format(ChannelName))
    overwrites = channel.overwrites_for(member.guild.default_role)
    overwrites.read_messages, overwrites.send_messages = False, False
    await channel.set_permissions(member.guild.default_role, overwrite=overwrites)
    overwrites = channel.overwrites_for(member)
    overwrites.send_messages, overwrites.read_messages = True, True
    await channel.set_permissions(member, overwrite=overwrites)
    await channel.send ('Success!!')

Upvotes: 3

Related Questions