Complete Server Lockdown Command Discord.py - python

I am trying to make a command which locks the whole server in discord.py but when I use the command, it just locks the channel which I used it in. Please help me if you can, Here is my current code.
#client.command()
#commands.has_permissions(manage_channels=True)
async def lockdown(ctx):
await ctx.channel.set_permissions(ctx.guild.default_role,send_messages=False)
embedVar3 = discord.Embed(
title="Locked",
description=(f'***Server has been locked.***'),
colour=(0x0000ff))
await ctx.send(embed=embedVar3)

You should be able to loop through each channel in the guild can lock each channel individually. Looking at the documentation, there doesn't seem to be a method for locking down the entire server.
#client.command()
#commands.has_permissions(manage_channels=True)
async def lockdown(ctx):
for channel in ctx.guild.channels:
channel.set_permissions(ctx.guild.default_role, send_messages=False)
...
Documentation

Related

Discord.py failing to send message to a specific channel in a cog/extension file

I just recently started using cogs/extension files for Discord.py and ran into some issue that I don't know how to fix. I'm trying to send a message in a specific channel but I always just get the error AttributeError: 'NoneType' object has no attribute 'send'. I know that I can send a message in a specific channel with:
#client.command()
async def test(ctx):
await ctx.send("test")
channel = client.get_channel(1234567890)
await channel.send("test2")
And this works perfectly fine in my "main file", but not in my "extension file", so it's not because of a wrong ID. The await ctx.send("test") works without problems as well, together with any other command I have, just the channel.send is causing troubles.
I'm importing the exact same libraries & co and otherwise should have the exact same "setup" in both files as well.
NoneType error means that it doesn't correctly recognize the channel. When you use get_channel you are looking for the channel in the bot's cache which might not have it. You could use fetch_channel instead - which is an API call.
#client.command()
async def test(ctx):
await ctx.send("test")
channel = await client.fetch_channel(1234567890)
await channel.send("test2")
As you know, the error occurs because your channel is not recognized. The solution is fetch_channel(channel_id). The problem lies in your channel = client.get_channel (1234567890).
Try adding await before client. Next replace get_channel with fetch_channel.
For general use consider your get_channel(), instead you need fetch_channel (channel_id) to retrieve an x.GuildChannel or x.PrivateChannel with the specified ID.
#client.command()
async def test(ctx):
await ctx.send("test")
channel = await client.fetch_channel(1234567890) #update
await channel.send("test2")

Suggest command not working in discord.py

I'm currently coding a bot in discord.py that does mod commands like kick, etc. I'm trying to make a command that you can suggest something to the server like they make a channel called suggestion-channel, when they do the command it sends it to that channel. I have a command that sends a message to a specific channel for bot suggestions, but that's the closest I've gotten. below is the code that I have for the suggest command in cogs, yes I use command handling.
#commands.command()
async def suggest(self ,ctx , *, suggestion):
embed = discord.Embed(title=f"Suggestion from {ctx.author.name}", description=suggestion, color=discord.Color.blue())
embed.set_footer(text=f"Suggestion created by {ctx.author}")
channel = discord.utils.get(message.server.channels, name="suggestion-channel")
message = await channel.send(embed=embed)
await message.add_reaction("✅")
await message.add_reaction("❌")
await ctx.message.delete()
await ctx.send("Thank you for your suggestion!")
First of all, you're not getting the channel in an on_message event, or using a user's message to get the server's channels. You'll have to replace message with ctx. Secondly, ctx.server is no longer valid since discord.py's migration. It would now be known as ctx.guild, which you can read about in their docs.
channel = discord.utils.get(message.server.channels, name="suggestion-channel")
# change this to:
channel = discord.utils.get(ctx.guild.channels, name="suggestion-channel")
Otherwise, if you find that any of your other commands are not working, you may have not processed your on_message event. You can view questions on how to solve this:
Why does on_message stop commands from working?
await process_commands(message) [docs]
discord.py #bot.command() not running

How to delete messages with discord bot

I made a small discord bot, and I need him to delete a message from the writer and then post something. When I use #bot.event it works fine but it's the only thing working in code(only the delete part works). And how to use ctx.message.delete?
#bot.command(aliases=["IDC"])
async def idc(ctx):
ctx.channel.send("https://cdn.discordapp.com/attachments/586526210469789717/838335535944564756/Editor40.mp4")
You just need to save the channel for later use.
#bot.command(aliases=["IDC"])
async def idc(ctx):
chn = ctx.channel
await ctx.message.delete()
await chn.send("https://cdn.discordapp.com/attachments/586526210469789717/838335535944564756/Editor40.mp4")
Your bot will need the delete messages permission.
EDIT:
This will also work, but it is recommended to use the first way as it can be faster.
#bot.command(aliases=["IDC"])
async def idc(ctx):
await ctx.send("https://cdn.discordapp.com/attachments/586526210469789717/838335535944564756/Editor40.mp4")
await ctx.message.delete()

How to create channel on_ready discord.py?

I was trying to create come channels when bot starts. I want them to create in function on_ready. But await guild.create_text_channel("Channel") requires guild by itself. Usually when I want to create it by command I make something like this:
#client.command()
async def create(ctx):
guild = ctx.guild
await guild.create_text_channel(name="test channel")```
But I need ctx to create guild. So the question is: How do I create a channel without ctx?
You simply need to get a discord.Guild instance, you can get that using Bot.get_guild
async def on_ready():
await client.wait_until_ready()
guild = client.get_guild(id_here)
await guild.create_text_channel(name="whatever")
If you want to create a channel in all the guilds the bot is in you can loop through client.guilds
async def on_ready():
await client.wait_until_ready()
for guild in client.guilds:
await guild.create_text_channel(name="whatever")
Reference:
Bot.get_guild
Bot.guilds
Ran into the same problem with disnake, and it took me reading through the api and testing things to figure it out. Since disnake is supposed to be forked off of discord.py try this
#commands.command(name="obvious", description="given")
async def command(ctx: Context):
await ctx.message.guild.create_text_channel("channel_name")
This solved my issue, and now I just need to add the parameters to properly sort channel creation.

How do I make the bot disconnect the voice channel with a command like -disconnect in lavalink.py? {CLOSED FOUND ALTERNATIVE METHOD}

I'm trying to make my discord bot disconnect the voice channel with a command like -disconnect using Lavalink. I have tried doing it in various approaches but it never seems to work for me. I also can't find many examples online. The most recent thing i've tried is this:
#commands.command()
async def leave(self, ctx):
guild_id = int(event.player.guild_id)
await self.connect_to(guild_id, None)
Tell me if you know how to make a disconnect command. Thanks!!!
This is discord.py rewrite btw.
To make a disconnect command is lavalink all you have to do is:
await self.connect_to(ctx.guild.id, None)
Here's an example:
import asyncio
#client.command()
async def disconnect(ctx):
channel = client.get_channel(channelIdHere)
vc = await channel.connect()
await asyncio.sleep(2) # Waits 2 seconds before leaving the vc
await vc.disconnect()
You should check the documentation for more information.

Categories