You should specify the guild in a different way, there is an example below.
guild = discord.utils.get(self.bot.guilds, id=ctx.guild.id)
role = discord.utils.get(guild.roles, name="MUTED")
and your second problem is you shouldn't make the "arg" into an int, if you do that you won't get the hour, day, week, or the month argument.
instead, you should use something like arg.endswith("h")
You don't need arg since you're already passing the duration in time.
Here is your fixed and slightly improved Code.
@commands.command()
@commands.guild_only()
@commands.has_permissions(manage_roles=True)
async def mute(self, ctx, member : discord.Member, time, *, reason=None):
role = discord.utils.get(member.guild.roles, name="MUTED")
await member.add_roles(role) # ? This for some reason doesnt work
timeSet = f"{time}"
if time.endswith("m"):
duration = int(time)
arg = duration * 60
time = "Months"
elif time.endswith("h"):
duration = int(time)
arg = int(duration * 60 * 60)
time = "Hours"
elif time.endswith("d"):
duration = int(time)
arg = int(duration * 60 * 60 * 24)
time = "Days"
elif time.endswith("y"):
duration = int(time)
arg = int(duration * 60 * 60 * 24 * 31 * 365)
time = "Years"
else:
arg = arg
await ctx.message.delete()
#Setting the embed for mutes
embedMuteServer=discord.Embed(title="MUTED", description="--------------------------------------------", color=0xff0000)
embedMuteServer.add_field(name="Muted user", value=f"{member}", inline=True)
embedMuteServer.add_field(name="For: ", value=f'{reason}', inline=True)
embedMuteServer.add_field(name="For: ", value=f'{timeSet} {time}', inline=True)
embedMuteServer.set_footer(text="-----------------------------------------------------")
#Setting up the sending embed
embedMute=discord.Embed(title="You were muted in", description=f"{member.guild.name}", color=0xff7800)
embedMute.add_field(name="Reason of the mute: ", value=f"{reason}", inline=False)
embedMute.add_field(name="The one who muted you was:", value=f"{ctx.message.author.mention}", inline=False)
embedMute.add_field(name="You were muted for:",value=f"{timeSet} {time}")
#Sending the embed
try:
await member.send(embed=embedMute)
await ctx.send(embed=embedMuteServer)
except:
pass
print("Server and DM embeds sent")
#Doing the command
await asyncio.sleep(arg)
await member.remove_roles(role)
#Sending unmuted DM
try:
embed=discord.Embed(title="You were unmuted!", description="---", color=0x33e639)
embed.add_field(name="Your muted timer has expired!", value="Try not to get muted again ", inline=False)
await member.send(embed=embed)
print("Unmute DM sent")
except discord.Forbidden:
pass
print("Cannot send messages to this user")
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…