Excuse me for my bad english :-)
I have a problem that a Discord bot I work on does not send private message to members after they join. I tried several solutions suggested in other Stack Overflow posts, but it doesn't work.
import discord
client = discord.Client()
#client.event
async def on_member_join(member):
print(f'Someone joined')
await member.send("You joined")
client.run("XXX")
But the function is never executed.
If I use the exact same code in a command like ?join, as in the following example, it works!
import discord
from discord.ext import commands
client = commands.Bot(command_prefix='?')
#client.command()
async def join(ctx):
member = ctx.message.author
print(f'Someone joined')
await member.send("You joined")
client.run("XXX")
So am I wrong thinking that on_member_join doesn't work anymore? What am I doing wrong?
Thanks for your time :-)
As of discord.py version 1.5+, Intents for bucket types of events must be stated by the bot prior to use. The event on_member_join needs the intent of discord.Intents.members to be set to True.
An easy way to do this is the following:
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix='?', intents=intents)
discord.py version 1.5 states that if the intents are not specified, it allows all intents except for Intents.members and Intents.presences.
More information on Intents can be found here: Gateway Intents in discord.py
Related
I came back to creating bots on discord, but this time with Python, it seems I am doing something wrong, and I don't know what it is.
I placed a simple code, which is (command-reply), but the bot does not answer and does nothing about it.
My bot is online, I don't get errors in my terminal, and I am using the correct token.
Here is my code:
import discord
intents = discord.Intents.default()
intents.members = True
client = discord.Client(intents=intents)
#client.event
async def on_message(message):
if message.content == "hello":
await message.channel.send("Hello there!")
client.run('BOT TOKEN')
You didn't enable the message_content intent.
Docs about intents: https://discordpy.readthedocs.io/en/stable/intents.html
I've been trying to make the bot send embeds but it's just not working, the code is executed with no errors but the embed doesn't get sent in the channel
#client.command()
async def embed(ctx):
embed=discord.Embed(
title='Title',
description='Description',
color=0x774dea
)
await ctx.send(embed=embed)
I already had the Privileged Gateway Intents enabled, but i added the intents to the code, same problem. Here is the full code:
import discord
from discord.ext import commands
from discord import Intents
_intents = Intents.default()
_intents.message_content = True
TOKEN = 'MYTOKENISHERE'
client = commands.Bot(command_prefix='$', intents=_intents)
#client.event
async def on_ready():
print('We Have logged in as {0.user}'.format(client))
#client.command()
async def embed(ctx):
embed=discord.Embed(
title='Title',
description='Description',
color=0x774dea
)
await ctx.send(embed=embed)
client.run(TOKEN)
It seems like you may not have the correct Discord Intents enabled for your bot.
Here may be a fix for your issue:
Go to the Discord Developer Portal and enable the options listed under Bot -> Privileged Gateway Intents
Note: If your bot reaches over 100 servers, you'll be required to apply for each of these
Image: here
Replace your current commands.Bot instantiation with something like this (focus on the _intents and intents=_intents portions):
from discord import Intents
from discord.ext import commands
_intents = Intents.default()
# Enable or disable the intents that are required for your project
_intents.message_content = True
bot = commands.Bot(command_prefix='*', intents=_intents)
# code here
bot.run(your_token_here)
If you're curious about customization of Intents and what you can do with them, read more here.
I'm trying to give all members in my server a role with this command, but it doesn't seem to work. It doesn't give the role to all the members, but just to the client. It also doesn't show any errors in the console.
#client.command()
async def assignall(ctx, *, role: discord.Role):
await ctx.reply("Attempting to assign all members that role...")
for member in ctx.guild.members:
await member.add_roles(role)
await ctx.reply("I have successfully assigned all members that role!")
Ensure that you have enabled the GUILD_MEMBERS Intent from your Discord Developer Portal and initialized your client with the proper intents.
from discord import Intents
from discord.ext.commands import Bot
client = Bot(intents=Intents.all())
•Go to https://discord.com/developers/applications
•Select your bot
•Go to the bot section
•Scroll down and enable all the intents(Technically you need only server members' intent but if you do all it will help you)
•Replace your client=commands.Bot(command_prefix="your_prefix") to
intents = discord.Intents.default()
intents.message_content = True
client=commands.Bot(command_prefix="your_prefix",intents=intents)
I want to create a discord.py bot that can mute and unmute itself. But I dont know how to get the VoiceState of the bot.
import discord, asyncio
from discord.ext import commands
bot = commands.Bot(command_prefix="/")
#bot.command()
async def unmute(ctx):
voice_client=ctx.guild.voice_client
channel=voice_client.channel
if ???ctx.channel.guild.voice_state???(channel=channel, self_deaf=True):
await ctx.channel.guild.change_voice_state(channel=channel, self_deaf=False)
await ctx.send("Unmuted!")
return
else:
await ctx.send("Error! Not muted!")
return
You need intents.voice_states enabled.
intents = discord.Intents.default() # enabling everything apart from privileged intents (members and presences)
bot = commands.Bot(command_prefix='/', intents=intents)
reference
Also, it's not ctx.channel.guild.voice_state(...), it's ctx.guild.voice_client or ctx.voice_client
I just started with discord.py and found out that on_member_join and on_member_remove don't work for me. Keep in mind that on_ready function works perfectly fine.
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix = "!")
#bot.event
async def on_ready():
print("""Bot ready""")
#bot.event
async def on_member_join(member):
channel = bot.get_channel(767370104814960640)
await channel.send(f"{member} has joined the server")
#bot.event
async def on_member_remove(member):
channel = bot.get_channel(766620766237753345)
await channel.send(f"{member} has left the server")
bot.run("my token")
So did I make a mistake with my code or something else went wrong?
discord.py 1.5.0 now supports discord API's Privileged Gateway Intents. In order to be able to exploit server data, you need to:
Enable Presence Intent and Server Members Intent in your discord application:
https://i.stack.imgur.com/h6IE4.png
Use discord.Intents at the beginning of your code:
intents = discord.Intents.all()
#If you use commands.Bot()
bot = commands.Bot(command_prefix="!", intents=intents)
#If you use discord.Client()
client = discord.Client(intents=intents)