Got a simple code that gives a role to members when they join the server. For some reason, it doesn't work. Here is the code:
#client.event
async def on_member_join(member):
role = discord.utils.get(member.guild.roles, name="Players")
await member.add_roles(role)
Doesn't give any errors, am I missing some sort of perms?
Related
I want that my discord bot sends a message on a specific channel and give a specific role everytime a new user joins the server
But my bot is doing nothing and I'm getting no errors
import discord
from keep_alive import keep_alive
class MyClient(discord.Client):
#Beim Einloggen
async def on_ready(self):
print("BOT is online")
async def on_member_join(member):
role = discord.utils.get(member.server.roles, id=<role_id>)
channel = MyClient.get_channel(<channel_id>)
await MyClient.add_roles(member, role)
await channel.send(f"Hello {member} nice to see you!")
Code for the user greeting (in a cog):
#commands.Cog.listener()
async def on_member_join(self, member):
channel = member.guild.system_channel
if channel is not None:
await channel.send(f"Welcome to the server {member.mention}!")
Basically, you are missing theese #commands.Cog.listener()'s
Try this! (This isn't used in a cog)
#client.event
async def on_member_join(member):
if member.guild.id !=<YOUR_GUILD_ID>:
return
welcomerole = discord.utils.get(guild.roles, name="<ROLE_NAME>")
await member.add_roles(welcomerole)
channel = client.get_channel(<WELCOME_CHANNEL_ID)
await channel.send("<WHAT_YOU_WANT>")
This makes it so when someone joins YOUR server it will send a custom message of your choice to the welcome channel
MAKE SURE YOU ADD YOUR GUILD ID IN <YOUR_GUILD_ID>
MAKE SURE YOU ADD A CUSTOM MESSAGE WHEN A MEMBER JOINS IN <WHAT_YOU_WANT>
MAKE SURE YOU ADD YOUR WELCOME CHANNEL ID IN <WELCOME_CHANNEL_ID>
MAKE SURE YOU ADD THE ROLE NAME THAT YOU GET WHEN A MEMBER JOINS IN <ROLE_NAME>
I tried to set up this bot event where it would give a role to the member that joins. For some reason, it doesn't give the role, but it doesn't give any error output either.
#client.event
def on_member_join(member):
role = discord.utils.get(member.server.roles, id="868708006504833034")
await client.add_roles(member, role)
#client.event
def on_member_join(member):
role = discord.utils.get(member.server.roles, id="868708006504833034")
await client.add_roles(member, role)
There are a few issues with your code
in line 2, the function is not async, discord.py events need to be async so replace def with async def
in line 3, ids are always ints, so you would make it an int, but you shouldn't even use discord.utils.get since you can just do member.guild.get_role(id). Note: id has to be int
in line 4, client.add_roles is outdated, it was replaced by member.add_roles. So you would have to change client.add_roles(member, role) to member.add_roles(role)
So the full updated code would be
#client.event
async def on_member_join(member):
role = member.guild.get_role(868708006504833034)
await member.add_roles(role)
Use this:
#client.event
async def on_member_join(member):
await member.add_roles(discord.utils.get(member.guild.roles, id=868708006504833034))
I think it can work correctly
ID is an integer:
role = discord.utils.get(member.server.roles, id=868708006504833034)
Try it.
I need help with my code, i wanted to make my bot sends a message on the first channel from the top when a particular member joins, but i got error with if member.id == *id*
#bot.event
async def on_member_join(member):
if member.id == *my id here, i just dont want to show it*
await message.channel.send('Nice guy joined the server!')
#bot.event
async def on_member_join(member):
if member.id == 660959014691143691:
await member.guild.text_channels[0].send('Nice guy joined the server!')
You haven't defined the message so you can't use message.channel.send. If you just want to send the first channel of the guild, that's simple to do with guild.text_channels.
#bot.event
async def on_member_join(member):
if member.id == *my id here, i just dont want to show it*
await member.guild.text_channels[0].send('Nice guy joined the server!')
I'm trying to send a welcome DM to users that join my discord server, but I have the bot setup in multiple servers. I'm trying to check the guild then send a message based on which guild it is in, but it's not working. I've looked and the popular question like this on stackoverflow uses commands and ctx, which cannot be used in on_member_join().
#client.event
async def on_member_join(member):
guild = client.get_guild(762921541204705321)
if guild == 762921541204705321:
await member.create_dm()
await member.dm_channel.send("Welcome!")
According to the documentation, when you call get_guild() it doesn't return the guild ID, it returns a Guild object. From the source code, it appears that this guild class does have its comparison operator overloaded, so it cannot deal with comparisons between a Guild object and an integer ID.
The solution to your problem is to just compare the number ID with the Guild.id attribute:
#client.event
async def on_member_join(member):
# client.get_guild returns a Guild object!
guild = client.get_guild(762921541204705321)
# Get the ID from the 'id' attribute on the guild object and compare.
if guild.id == 762921541204705321:
await member.create_dm()
await member.dm_channel.send("Welcome!")
I want to create a discord bot that, after receiving a message from the admin, changes the role of all server members (excluding the admin) from #everyone to #newrole.
I tried this code but there is an error:
role = discord.utils.get(server.roles, name="newrole")
AttributeError: 'NoneType' object has no attribute 'roles'
I use the latest version of discord and Python 3.6.
server = client.get_guild(int(server-id))
#client.event
async def on_message(message):
if message.content.startswith('change role'):
await message.author.send("Change the role from #everyone to #newrole")
role = discord.utils.get(server.roles, name="newrole")
for member in server.members:
await member.add_roles(role)
All members have the everyone role at all times. Members can have multiple roles, however, so you can also give them your new role. There is no coroutine for bulk assigning roles, so you have to do it member by member:
#client.event
async def on_message(message):
if message.content.startswith('change role'):
await message.author.send("Change the role from #everyone to #newrole")
role = discord.utils.get(message.guild.roles, name="newrole")
for member in message.guild.members:
await member.add_roles(role)