I am getting an error running this discord bot code. Can anyone help?

> import os
> import discord
> from discord.ext import commands
> import traceback
> import sys
> import asyncio
> from typing import Union
> from discord import utils
> from discord.errors import Forbidden
> 
> intents = discord.Intents.default() #IMPORTANT:Enable all Privileged Gateway Intents in https://discord.com/developers/applications/ 
> intents.members = True
> 
> TOKEN="OTgwMTQ0NDQ1NjI4NDk3OTcw.G89OAI.55URWtsMgkzVEeplQoctLVPu9BMF4u1rP7Ah5g" #example - "bjqfebjlnqklnifbnaewnhnfoiqefioqip"
> PREFIX=";" #put your prefix here
> 
> bot = commands.Bot(command_prefix =PREFIX,
>                    intents=intents,
>                    help_command=None,
>                    case_insensitive=True)
> 
> #mute role creation
> async def create_mute_role(guild):
>     perms = discord.Permissions(send_messages=False)
>     mute_role = await guild.create_role(name="Muted", color=discord.Color.dark_grey(), permissions=perms)
> 
>     for channel in guild.channels:
>         await channel.set_permissions(mute_role, send_messages=False)
> 
>     return mute_role
> 
> warn_count = {}
> 
> @bot.command(name="warn")
> @commands.has_guild_permissions(kick_members=True)
> async def warn(ctx, user: discord.Member = None, *, reason=None):
>     if user is None or reason is None:
>         await ctx.send("Insufficient arguments.")
>     elif ctx.author.top_role.position <= user.top_role.position and ctx.guild.owner.id != ctx.author.id:
>         await ctx.send("You cannot warn this user because their role is higher than or equal to yours.")
>     else:
>         print(f"Warning user {user.name} for {reason}...")
> 
>         if str(user) not in warn_count:
>             warn_count[str(user)] = 1
>         else:
>             warn_count[str(user)] += 1
> 
>         embed = discord.Embed(
>             title=f"{user.name} has been warned", color=discord.Colour.red())
>         embed.add_field(name="Reason", value=reason)
>         embed.add_field(name="This user has been warned",
>                         value=f"{warn_count[str(user)]} time(s)")
> 
>         await ctx.send(content=None, embed=embed)
> 
> @bot.command(name="warncount")
> async def warncount(ctx, user: discord.Member):
>     if str(user) not in warn_count:
>         warn_count[str(user)] = 0
> 
>         count = warn_count[str(user)]
>         await ctx.send(f"{user.mention} has been warned {count} time(s)")
> 
> @bot.command(name="mute")
> @commands.has_guild_permissions(kick_members=True)
> async def mute(ctx, user: discord.Member = None, time: str = None):
>     if user is None:
>         await ctx.send("Insufficient arguments.")
>     elif ctx.author.top_role.position <= user.top_role.position and ctx.guild.owner.id != ctx.author.id:
>         await ctx.send("You cannot mute this user because their role is higher than or equal to yours.")
>     else:
>         guild = ctx.guild
>         mute_role = None
> 
>         for role in guild.roles:
>             if role.name.lower() == "muted":
>                 mute_role = role
>                 break
> 
>         if mute_role in user.roles:
>             await ctx.send("This user is already muted.")
>         else:
>             if not mute_role:
>                 await ctx.send("This server does not have a `Muted` Role. Creating one right now.")
>                 await ctx.send("This may take some time.")
>                 mute_role = await create_mute_role(guild)
> 
>             if time is None:
>                 await user.add_roles(mute_role)
>                 await ctx.send(f"User {user.mention} has been muted! They cannot speak.")
>             else:
>                 time_unit = None
>                 parsed_time = None
> 
>                 if "s" in time:
>                     time_unit = "seconds"
>                     parsed_time = time[0:(len(time) - 1)]
>                 elif "m" in time:
>                     time_unit = "minutes"
>                     parsed_time = time[0:(len(time) - 1)]
>                 elif "h" in time:
>                     time_unit = "hours"
>                     parsed_time = time[0:(len(time) - 1)]
>                 else:
>                     time_unit = "minutes"  # default to minutes if user doesn't provide a time unit
>                     parsed_time = time[0:len(time)]
> 
>                 await user.add_roles(mute_role)
>                 await ctx.send(f"User {user.mention} has been muted for {parsed_time} {time_unit}! They cannot speak.")
> 
>                 if time_unit == "seconds":
>                     await asyncio.sleep(int(parsed_time))
>                 elif time_unit == "minutes":
>                     await asyncio.sleep(int(parsed_time) * 60)
>                 elif time_unit == "hours":
>                     await asyncio.sleep(int(parsed_time) * 3600)
> 
>                 await user.remove_roles(mute_role)
>                 await ctx.send(f"User {user.mention} has been unmuted after {parsed_time} {time_unit}! They can speak now.")
> 
> @bot.command(name="unmute")
> @commands.has_guild_permissions(kick_members=True)
> async def unmute(ctx, user: discord.Member = None):
>     if user is None:
>         await ctx.send("Insufficient arguments.")
>     elif ctx.author.top_role.position <= user.top_role.position and ctx.guild.owner.id != ctx.author.id:
>         await ctx.send("You cannot unmute this user because their role is higher than or equal to yours.")
>     else:
>         guild = ctx.guild
>         mute_role = None
> 
>         for role in guild.roles:
>             if role.name.lower() == "muted":
>                 mute_role = role
>                 break
> 
>         if mute_role in user.roles:
>             if not mute_role:
>                 mute_role = await create_mute_role(guild)
> 
>             await user.remove_roles(mute_role)
>             await ctx.send(f"User {user.mention} has been unmuted! They can now speak.")
> 
>         else:
>             await ctx.send("This user was never muted.")
> 
> bot.run(os.environ('TOKEN')

I hope that is not your actual bot token, if it’s so your bot is compromised and you need to regenerate it

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.