Lock command discord.py - python

I made a lock command but when fl.lock #general test is used in #general channel it isn't locking the general channel but when fl.lock #general test is used in #staff-chat channel it is locking the #general channel
So my bot isn't locking a channel when that channel is mentioned in the lock commmand and it is used in the same channel
The code
#commands.command(case_insensitive = True)
#commands.has_any_role(885434191783788564, 856071314740740096, 856061783722426378, 856465667296985108)
async def lock(self, ctx, channel: discord.TextChannel, *, reason=None):
channel = channel or ctx.channel
overwrite = channel.overwrites_for(ctx.guild.default_role)
if overwrite.send_messages == True:
overwrite.send_messages = False
await channel.set_permissions(ctx.guild.default_role, overwrite=overwrite)
em_reason = discord.Embed(title="**Channel locked**", description=f":lock: {reason}",color=discord.Color.red())
embed = discord.Embed(description = f"<a:fl_check:874522235879186483> Locked channel <#{channel.id}>",color=discord.Color.green())
await ctx.send(embed=em_reason)
await ctx.send(embed=embed)
elif overwrite.send_messages == False:
em = discord.Embed(description="<a:fl_no:874522273984442420> That channel is already locked.", color=discord.Color.red())
await ctx.send(embed=em)```

I think channel = channel or ctx.channel is the issue here.
You should check directly if the channel argument is None and than use the ctx.channel.
#commands.command(case_insensitive = True)
#commands.has_any_role(885434191783788564, 856071314740740096, 856061783722426378, 856465667296985108)
async def lock(self, ctx, channel: discord.TextChannel=None, *, reason=None):
if channel is None:
channel = ctx.channel
overwrite = channel.overwrites_for(ctx.guild.default_role)
if overwrite.send_messages == True:
overwrite.send_messages = False
await channel.set_permissions(ctx.guild.default_role, overwrite=overwrite)
em_reason = discord.Embed(title="**Channel locked**", description=f":lock: {reason}",color=discord.Color.red())
embed = discord.Embed(description = f"<a:fl_check:874522235879186483> Locked channel <#{channel.id}>",color=discord.Color.green())
await ctx.send(embed=em_reason)
await ctx.send(embed=embed)
elif overwrite.send_messages == False:
em = discord.Embed(description="<a:fl_no:874522273984442420> That channel is already locked.", color=discord.Color.red())
await ctx.send(embed=em)

Related

Why is my bot.command function not working?

So basically I have this problem, any command that I type does not work. There are no errors, and everything else is working fine. It's just that for some reasons #bot.command() isn't working, and that is kind of annoying.
import discord
from discord.utils import get
from discord.ext import commands
import time
intents = discord.Intents.default()
intents.members = True
intents.message_content = True
bot = commands.Bot(command_prefix = '$', intents=intents)
TOKEN = 'hi'
ROLE = 'hi'
db1 = [hi, hi]
#bot.command()
async def test(ctx, arg):
await ctx.send(arg)
await bot.process_commands(arg)
#bot.event
async def on_member_join(member):
role = get(member.guild.roles, name=ROLE)
await member.add_roles(role)
await member.send('hi')
try:
channel = member.guild.system_channel
embedVar = discord.Embed(title="Welcome <#{}> in {} ".format(str(member.id),str(member.guild)), description="hi", color=0x00ff00)
await channel.send(embed=embedVar)
except:
channel = member.guild.get_channel(hi)
embedVar = discord.Embed(title="Welcome <#{}> in {} ".format(str(member.id),str(member.guild)), description="hi", color=0x00ff00)
await channel.send(embed=embedVar)
#bot.event
async def on_member_remove(member):
try:
channel = member.guild.system_channel
embedVar = discord.Embed(title="Bye {} from {} ".format(str(member.name),str(member.guild)), description="Come back when you want", color=0x00ff00)
await channel.send(embed=embedVar)
except:
channel = member.guild.get_channel(hi)
embedVar = discord.Embed(title="Bye {} from {} ".format(str(member.name),str(member.guild)), description="Come back when you want", color=0x00ff00)
await channel.send(embed=embedVar)
#bot.event
async def on_invite_create(invite):
channel = bot.get_channel(hi)
await channel.send("An invite has been created, {}, by <#{}> on {}".format(str(invite.url), str(invite.inviter.id), str(invite.created_at)))
#bot.event
async def on_invite_delete(invite):
channel = bot.get_channel(hi)
await channel.send("An invite has been deleted by{}".format(str(invite.inviter.id)))
#bot.event
async def on_member_ban(guild, member):
channel = bot.get_channel(hi)
embedVar = discord.Embed(title="Ban", description="Ban requested on<#{}>".format(str(member.id)))
await channel.send(embed=embedVar)
#bot.event
async def on_member_unban(guild, member):
channel = bot.get_channel(hi)
embedVar = discord.Embed(title="Unban", description="Unban requested on<#{}>".format(str(member.id)))
await channel.send(embed=embedVar)
#bot.event
async def on_ready():
print(f'{bot.user} succesfully logged in')
return
#bot.event
async def on_message(message):
if message.content.startswith('purge requested by'):
time.sleep(1)
await message.delete()
if message.author == bot:
return
if message.content == 'hi':
await message.channel.send('hi')
if message.content.startswith('binvites'):
totalInvites = 0
for i in await message.guild.invites():
if i.inviter == message.author:
totalInvites += i.uses
await message.channel.send(f"You have invited {totalInvites} member{'' if totalInvites == 1 else 's'} to the Central Trade server")
if message.content == 'bpurge':
if message.author.id in db1:
await message.channel.purge(limit=10)
await message.channel.send('purge requested by <#{}>'.format(str(message.author.id)))
else:
return
if message.content == 'block':
if message.author.id in db1:
channel = message.channel
overwrite = channel.overwrites_for(message.guild.default_role)
overwrite.send_messages = False
await channel.set_permissions(message.guild.default_role, overwrite=overwrite)
embedVar = discord.Embed(title="Lock", description="Channel lock request by <#{}>".format(str(message.author.id)), color= 0x00FFFF)
await message.channel.send(embed=embedVar)
else:
await message.author.send('You do not have the permission to use this command')
if message.content == 'bunlock':
if message.author.id in db1:
channel = message.channel
overwrite = channel.overwrites_for(message.guild.default_role)
overwrite.send_messages = True
await channel.set_permissions(message.guild.default_role, overwrite=overwrite)
embedVar = discord.Embed(title="Unlock", description="Channel unlock request by <#{}>".format(str(message.author.id)), color=0xC0C0C0)
await message.channel.send(embed=embedVar)
else:
await message.author.send('You do not have the permission to use this command')
if message.content == 'test':
embedVar = discord.Embed(title="test", description="test", color=0x00ff00)
embedVar.add_field(name="test", value="test", inline=False)
embedVar.add_field(name="test", value="test", inline=False)
await message.channel.send(embed=embedVar)
if message.content == 'bpurges':
if message.author.id in db1:
await message.channel.purge(limit=10000)
await message.channel.send('purge requested by <#{}>'.format(str(message.author.id)))
embedVar = discord.Embed(title="Purge", description="Purge requested by<#{}>".format(str(message.author.id)))
await message.channel.send(embed=embedVar)
time.sleep(1)
await message.channel.delete()
bot.run(TOKEN)
Anyone has an idea why it's not working ? Also seems like i need to post more details, don't pay attention to this : Romรฉo et Juliette (Romeo and Juliet) est une tragรฉdie de William Shakespeare.

Leave selected channels out of discord.py loop

I have created a bot that will lock every channel when I type the command, however, I don't want it to lock the staff ones and the announcement ones.
Here is my code so far:
#bot.command(aliases=['r'])
#commands.has_permissions(manage_channels=True)
async def lockdown(ctx):
for guild in bot.guilds:
for channel in guild.text_channels:
channel = channel
overwrite = channel.overwrites_for(ctx.guild.default_role)
overwrite.send_messages = False
await channel.set_permissions(ctx.guild.default_role, overwrite=overwrite)
await channel.send('Lockdown has started')
How can I do this?
You can exclude channels which you dont want to lock by name using a blocking statement. Like this:
#bot.command(aliases=['r'])
#commands.has_permissions(manage_channels=True)
async def lockdown(ctx):
for guild in bot.guilds:
for channel in guild.text_channels:
if channel.name in ['dont_lock_ch_name_1', 'dont_lock_ch_name_2']:
continue
overwrite = channel.overwrites_for(ctx.guild.default_role)
overwrite.send_messages = False
await channel.set_permissions(ctx.guild.default_role, overwrite=overwrite)
await channel.send('Lockdown has started')

Adding Channel Overwrites for user in Discord.py

I am trying to create a channel overwrite for my bot so it can send the message in the channel that the channel is locked for the #everyone role
#commands.command()
#has_permissions(manage_channels=True)
async def lock(self, ctx):
member = discord.Member.id(712885407993561169)
await ctx.channel.set_permissions(ctx.guild.default_role, send_messages=False, read_messages=False)
await ctx.channel.set_permissions(member, send_messages=True, read_messages=True)
await ctx.send(':greentick: Locked down ' + ctx.channel.name)
You can try:
#commands.command()
#commands.has_permissions(manage_channels=True)
async def lock(self, ctx: commands.Context):
overwrites = ctx.channel.overwrites_for(ctx.guild.default_role)
overwrites.read_messages, overwrites.send_messages = False, False
await ctx.channel.set_permissions(ctx.guild.default_role, overwrite=overwrites)
overwrites = ctx.channel.overwrites_for(ctx.me)
overwrites.send_messages, overwrites.read_messages = True, True
await ctx.channel.set_permissions(ctx.me, overwrite=overwrites)

Discord py - if emoji in new created channel - delete the channel

Hey guys!
I am trying to make a litte ticket-bot for my server - the problem is that the emoji in the reaction deletes a channel!
So if a member uses the emoji outside the new created channel it will delete the channel :(
What does my code look like now:
#commands.Cog.listener()
async def on_raw_reaction_add(self, payload):
guild_id = payload.guild_id
guild = self.client.get_guild(guild_id)
user_id = payload.user_id
user = self.client.get_user(payload.user_id)
message_id = payload.message_id
emoji = payload.emoji.name
if message_id == 768765161301213185 and emoji == "๐Ÿ“ฉ":
member = discord.utils.find(lambda m : m.id == payload.user_id, guild.members)
support_role = guild.get_role(760792953718308915)
category = guild.get_channel(768764177736400906)
overwrites = {
guild.default_role: discord.PermissionOverwrite(read_messages=False),
member: discord.PermissionOverwrite(read_messages=True, send_messages=True),
support_role: discord.PermissionOverwrite(read_messages=True, send_messages=True)
}
ticket_nr = random.randint(100,999)
channel = await category.create_text_channel(f'ticket-{ticket_nr}', overwrites=overwrites)
embed = discord.Embed(
title="How can I help you?",
description="Please wait for a supporter.")
embed.set_author(name="TiLiKas Ticket Bot")
for channel_all in guild.text_channels:
if str(channel_all) == str(channel):
if user_id != 739740219544305714 and emoji == "๐Ÿ”’":
await channel.delete(reason=None)
else:
print("ERROR")
What do I want?
I want the bot only to responde on the emoji if its used in the new created channel!
https://discordpy.readthedocs.io/en/latest/ext/commands/api.html?highlight=check#discord.ext.commands.Bot.wait_for
#client.event
async def on_message(message):
if message.content.startswith('$thumb'):
channel = message.channel
await channel.send('Send me that ๐Ÿ‘ reaction, mate')
def check(reaction, user):
return user == message.author and str(reaction.emoji) == '๐Ÿ‘'
try:
reaction, user = await client.wait_for('reaction_add', timeout=60.0, check=check)
except asyncio.TimeoutError:
await channel.send('๐Ÿ‘Ž')
else:
await channel.send('๐Ÿ‘')
Your check method should look something like:
def check(reaction, user):
return user == message.author and str(reaction.emoji) == '๐Ÿ‘' and reaction.message.channel == message.channel
This check here, checks:
the user who is reacting to the message is the same user who started the command.
the reaction is a thumbs up emoji.
the reaction is done in the same channel the command was started in.

How to take input of a mentioned channel and send message in that channel?

I have the following code to generate embed from a message , it works fine now what I want is after creating embed the bot should ask the user to mention a channel and after the user mentions a channel , the bot should send that embed there. How do I do it?
#bot.command()
async def embed(ctx):
await ctx.send("Enter title for embed:")
e = await get_input_of_type(str, ctx)
await ctx.send("Enter the content for embed:")
c = await get_input_of_type(str, ctx)
embed = discord.Embed(
title = e,
description = c,
color = 0x03f8fc,
timestamp= ctx.message.created_at
)
embed.set_thumbnail(url = ctx.guild.icon_url)
await ctx.channel.send(embed=embed)
Using wait_for() and TextChannelConverter
#bot.command()
async def embed(ctx):
def check(m):
return m.author == ctx.author and m.channel == ctx.channel
await ctx.send("Enter title for embed:")
e = await get_input_of_type(str, ctx)
await ctx.send("Enter the content for embed:")
c = await get_input_of_type(str, ctx)
embed = discord.Embed(
title = e,
description = c,
color = 0x03f8fc,
timestamp= ctx.message.created_at
)
embed.set_thumbnail(url = ctx.guild.icon_url)
channel = await bot.wait_for("message", check=check)
channel = await commands.TextChannelConverter().convert(ctx, channel.content)
await channel.send(embed=embed)
I used the Client.wait_for() coroutine to wait for a message from the user. Then I formatted the message.content string to get just the ID of the channel. Then I used the discord.utils.get method to get the channel using just the ID.
# at the top of the file
from discord.utils import get
#client.command()
async def embed(ctx):
# embed stuff here
def check(msg):
# make sure the author of the message we're waiting for is the same user that invoked the command
return ctx.author == msg.author
await ctx.send("mention a channel to send to")
msg = await client.wait_for('message', timeout=60.0, check=check)
msg = msg.content.split("#")[1].split('>')[0]
"""
over here we are spliting the '#' from the message content first and index just the ID with the '>'
(if the message is just a channel mention it shoud be something like this: <#000000000000000000>)
then we split the '>' from the string and index just the ID part and now we have the ID of the channel
"""
channel = get(ctx.guild.channels, id=int(msg))
await channel.send(f'{words}')

Categories

Resources