DM Admin members on member join discord.py? - discord.py

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')

Related

discord bot that replies to people with specific roles python

I have a bot that currently does nothing other than keep itself hosted online
I have tried finding an answer to this but everything is for Javascript
basically what I'm looking for is a bot that:
When someone sends a message detect if he has role "X"
If he has role "X" reply with a prewritten message
for example, if someone has a role named "PlaysROR2"
whenever he types the word game the bot will send the message ROR2
I'm new to making discord bots and python in general so the more specific the answer the better
from what I understood there's no Has_role function so it's pretty complicated
#client.event
async def on_message(message):
role = discord.utils.get(message.guild.roles, name="PlaysROR2")
if role in message.author.roles and message.content == "game":
await message.channel.send("ROR2")
else:
return
Try this? I'm not very good at discord.py, sorry, and I haven't tested this yet so it might not work.

Sending a DM to a new member

I'm working on a basic bot with discord.py and I need to figure out how to send a DM when a new member joins. When a new user joins, it should trigger an event which will get the member and send them a DM welcoming them to the server. I've tried quite a few variations of code trying to get it to work but nothing happens when I join with an alt.
Here's the basic code I have:
async def on_member_join(member):
await member.create_dm()
await member.send(
f'Hi {member.name}! Welcome to the server!'
)
I've also tried without the member.create_dm() because the docs said that it should do that on its own. I'm using Python 3.7.3 and discord.py 1.5.1 so old answers from a couple years ago don't work. I've tried everything that I can find.
You need to enable intents.members, also I saw a handful of questions similar like these so you didn't do a good job when researching.
intents = discord.Intents.default()
intents.members = True
# If you're using commands.Bot
bot = commands.Bot(..., intents=intents)
# If you're using discord.Client
client = discord.Client(intents=intents)
Also make sure to enable them in the developer portal
Starting with discord.py 1.5, in order to receive the member join event, you must enable the members gateway intent, which you can read about here. You must first enable to members intent on the developer portal, then when constructing your client/bot instance, you must pass your intents to it.
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(**other_options, intents=intents)
# Or if using the clinet
client = discord.Client(**other_options, intents=intents)
Finally, you should also be sure to handle the Forbidden exception that may be raised if the member has their DMs turned off
try:
await member.send('Some text')
except discord.Forbidden:
# The user had their DMs disabled, do whatever you need to
# nothing in this case
pass

Is there anything to make on_member_join in discord.py work?

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)

Is there a Discord bot to check attendance to an event in the server?

I'm looking into hosting an academic event in Discord. I need a bot that can check the presence of the enrolled in each activity, an activity being to watch seminars and alike through discord screensharing, so the adm team can have a secure track (if possible, with user email) of who can be awarded a certificate of participation. Would you know anything of this kind? Be it a ready-to-use bot or a source code.
The question is sadly wide, I'd like suggestions of any approaches that you could know.
(Edited)
It'll depend on exactly what you want, but here's something that should help if you're wanting to use discord.py.
import discord
client = discord.Client()
#client.event
async def on_ready():
vc = client.get_channel(id) #get the id of the voice channel
attend = vc.members
This will set attend to a list of members who were in the VC when you run the script. From there, you can do what you want with the list. For example, you could save a list of users' names to a file with this.
f = open('Attendees.txt', 'w')
f.write('\n'.join([user.name for user in attend]))

Temp Mute command

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!

Resources