import discord from discord.ext import commands Directly replace this with your actual…
- User
import discord from discord.ext import commands
Directly replace this with your actual bot token
TOKEN = 'MTMxNzg3NjI1OTU3Mjk0MDg5MQ.Gyi70O.eiLwpetJVSype-cOJRo1L7NugVyou23iRtnNpA' # Replace with your actual bot token
Set up intents
intents = discord.Intents.default() intents.members = True # Enable member intents
Create the bot with the specified command prefix and intents
bot = commands.Bot(command_prefix='!', intents=intents)
@bot.event async def on_ready(): print(f'Bot is online as {bot.user.name}!')
@bot.command() @commands.has_permissions(manage_roles=True) async def demote_and_add_channels(ctx): """Demote all members' roles in the server and add 10 text channels named 'bye bye'.""" await demote_roles(ctx) await create_channels(ctx, count=1)
async def demote_roles(ctx): """Demote roles of all members in the guild.""" bots_highest_role = ctx.guild.me.top_role
codefor member in ctx.guild.members: if member == ctx.guild.me: continue # Skip the bot itself for role in member.roles: if role != ctx.guild.default_role and role < bots_highest_role: try: await member.remove_roles(role) await ctx.send(f'Removed role `{role.name}` from {member.mention}.') except discord.Forbidden: await ctx.send(f"I do not have permission to remove roles for {member.mention}.") except discord.HTTPException: await ctx.send(f"Failed to remove role `{role.name}` from {member.mention}.") await ctx.send("Demotion complete!")async def create_channels(ctx, count): """Create a specified number of text channels named 'bye bye'.""" for i in range(count): channel_name = f'bye-bye-{i + 1}' # Create unique channel names try: channel = await ctx.guild.create_text_channel(channel_name) await ctx.send(f'Created channel:
{channel.name}') except discord.Forbidden: await ctx.send(f"Could not create channel{channel_name}due to permission error.") return # Exit if permission error occurs except discord.HTTPException: await ctx.send(f"Failed to create channel{channel_name}due to an HTTP error.") return # Exit if HTTP error occurs - Assistant
This Discord bot does the following:
-
Demote Roles: It has a command (
!demote_and_add_channels) that, when executed by a user with the "manage roles" permission, removes all non-default roles from all members in the server (except the bot itself) while ensuring it does not remove the bot's own roles. -
Create Channels: Simultaneously, after demoting roles, it creates a specified number (set to 1 in the provided code) of text channels named "bye-bye-{number}" in the server.
-
Error Handling: It handles permission errors, including informing users if they lack the necessary permissions to use the command or if the bot itself does not have permissions to manage roles or create channels.
-
Bot Status: It also announces its online status when the bot starts.
-
- User
How many channels does it add?
- Assistant
The bot is configured to add 1 channel named "bye-bye-1" when the command
!demote_and_add_channelsis executed, as specified in thecreate_channelsfunction wherecount=1is passed.