I am trying to make it so server moderators are able to temporarily mute users in their Discord. I am not an experienced developer within Discord bots but learning.
What I am trying to do?
I am trying to make it so server owners are able to temp mute users in their Discord servers.
This is what I currently have for my Mute command:
#bot.command(pass_context=True)
async def mute(ctx, user: discord.Member):
if ctx.message.author.server_permissions.kick_members:
role = discord.utils.get(user.server.roles, name="Muted")
embed = discord.Embed(title="{} has been muted!".format(user.name), description="When the user needs unmuting do !unmute #user!" , color=0x0072ff)
embed.set_footer(text="Reversed by Damian#9209 | Reduction#9975")
embed.set_thumbnail(url=user.avatar_url)
await bot.add_roles(user, role)
await bot.say(embed=embed)
else:
embed = discord.Embed(title="Permission Denied.", description="You don't have permission to use this command.", color=0xff0000)
embed.set_footer(text="Reversed by Damian#9209 | Reduction#9975")
await bot.say(embed=embed)
As I am sure you are aware, the Discord.py library has changed to no longer include the rewrite branch. This means that the mute command has been made way easier for you. I'll provide some code to get you started. I will also provide links to all the documentation to each line.
#bot.command(name="tempmute",description="Temporarily mute a member")
#commands.has_permission(mute_members=True)
async def _tempmute(ctx,user:discord.Member):
muteRole = discord.utils.get(ctx.guild.roles,name="Muted")
await user.add_roles(muteRole)
has_permission is a check for Discord user permissions
utils.get is a utility function that can step through iterables and find the correct search filter. Here, we are using it to find a role with the name "Muted"
add_roles is a coroutine function (aka needs to be awaited) that adds one or multiple roles to a user.
I suggest also making sure that the role has the speak permission disabled so that you don't have to deal with it through the on_message event. Hope that helps!
Related
import discord
from discord.ext import commands
bot = discord.Bot(command_prefix='!')
#bot.event
#commands.has_permissions(administrator=False)
async def delmessage(message):
await message.delete()
As explained by stijndcl in comment, the best solution is to go directly in your server settings to allow only admins and moderators to use #everyone and #here.
If you still you want to use a bot, something can easily be done using the on_message handler and then checking the roles of the message author to delete or keep the message.
The problem with this method is that the filtering process is done after the message publication, so deleting an unauthorized ping will create ghost pings and therefore a poor experience for your server members (and again why bother when a setting exists for this exact purpose).
I'm currently programming a bot and my first goal is to make the bot welcome and say goodbye to members that join or leave, but the bot sends nothing, not even showing any error.
#client.event
async def on_member_join(member):
print(" System: A member has joined the server!")
def sprint(str):
for c in str + '\n':
sys.stdout.write(c)
sys.stdout.flush()
time.sleep(3./90)
sprint(" Bot: Oh hey, looks like "+member.name+" has joined..")
#send message to a specific channel
channel = client.get_channel(channel id from the welcome channel)
await channel.send("Hey! Welcome to the server, "+member.mention+"! I hope you read the #rules before doing anything, otherwise have fun and enjoy your stay!")
I did it last time before I recreated the script, but the on_member_join thing doesn't work, not even on_member_leave! (I'm doing the on_member_join first to check if it's working)
My alternate account rejoined my server, but the bot (except MEE6) didn't send anything.. Is there anything you guys can help me?
You'll need to enable the member intent by creating an instance of discord.Intents.default, setting Intents.members to True, and passing the instance into your commands.Bot or discord.Client constructor, like:
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(intents=intents)
You'll also need to enable the members intent in the Discord dev portal (https://discord.com/developers). Go to that url, click on your application, go to the Bot tab, scroll down to the 'privileged gateway intents' section, and enable the members intent.
Then, restart your bot, and member events should work.
(for more info, look at https://discordpy.readthedocs.io/en/latest/intents.html)
So basically what I am doing is trying to write a very basic kick and ban command to use for my discord bot. I've looked through many different tutorials and similar questions asked, but I simply cannot find a fix to this.
#bot.command()
#commands.has_permissions(kick_members=True)
async def kick(ctx, user: discord.Member, *, reason=None):
await ctx.kick(reason=reason)
await ctx.send(f"{user} has been kicked successfully")
When I have the bot up and running, everyone is able to kick everybody else regardless of whether they have permissions to kick members or not, even though I have specified it to check if the user is able to kick people. I want it so that if a user does not have permissions to kick members, they shouldn't be able to kick anyone. I am fairly new to coding, and any help is appreciated. If anyone would like to check for the rest of the code in my program, I am happy to post it, but for now I don't see a reason to have to.
await ctx.kick(reason=reason)
You're trying to kick the context. Kick the member instead:
await user.kick(reason=reason)
I'm trying to make a discord command where users can change what my bot is playing, but it changes for all servers and I don't want that. Is there a way to make it so that it only changes for the server that the user is sending the command from?
So far all I have is this
games={}
#bot.command(pass_context=True)
async def game(ctx, name):
server=ctx.message.server
games[server.name] = await bot.change_presence(game=discord.Game(name=name))
This is not possible as a Discord bot is only considered to have one websocket. You could make a loop and asyncio.sleep(time) to change the presence over time. Hope that helps!
So I am playing around with coding a bot, but am a complete newb to coding. I want the bot to DM all members of the Admin role when someone joins the server. I have looked everywhere and cannot get my head around it. I have tried multiple ways with no success. Has anyone get any ideas?
You'll have to loop through everyone on the server and check if they are an administrator
import discord
client = discord.Client()
#client.event
async def on_member_join(member):
server = member.server
for user in server.members:
if user.server_permissions.administrator:
await client.send_message(user, "A new member has joined")
client.run('token')