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.
Related
I'm trying to understand how migrating from discord.py version 1.7.3 to 2.0 works. In particular, this is test code I'm using:
from discord.ext import commands
with open('token.txt', 'r') as f:
TOKEN = f.read()
bot = commands.Bot(command_prefix='$', help_command=None)
#bot.event
async def on_ready():
print('bot is ready')
#bot.command()
async def test1(ctx):
print('test command')
bot.run(TOKEN)
In discord.py 1.7.3, the bot prints 'bot is ready', and I can do the command $test1.
In discord.py 2.0, the bot prints 'bot is ready', but I can't do the command, and there isn't any error message in the console when I'm trying to do the command.
Why does this occur, and how can I restore the behaviour of version 1.7.3 in my bot?
Use Intents with discord.py
Enable Intents
On Discord Developer Portal
Select your application
Click on the Bot section
And check MESSAGE CONTENT INTENT
Add your intents to the bot
Let's add the message_content Intent now.
import discord
from discord.ext import commands
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='$', intents=intents, help_command=None)
Put it together
The code should look like this now.
import discord
from discord.ext import commands
with open('token.txt', 'r') as f: TOKEN = f.read()
# Intents declaration
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='$', intents=intents, help_command=None)
#bot.event
async def on_ready():
print('bot is ready')
# Make sure you have set the name parameter here
#bot.command(name='test1', aliases=['t1'])
async def test1(ctx):
print('test command')
bot.run(TOKEN)
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'm making a Discord bot and want to use guild.members to get a list of all the members. I encountered a problem which is that guild.members is only returning the bot itself, and not any member in the server.
What I tried was to enable intents in my code and in the Discord developers portal but it still did not make a change.
My issue it that I'm trying to use guild.members using a user token but it isn't working.
My code:
import discord
from discord.ext import commands
from discord.ext.commands import bot
from discord.flags import Intents
intents = discord.Intents().all()
bot = commands.Bot(command_prefix='!',intents=intents)
guild = bot.get_guild(790189441695350807)
#bot.event
async def on_ready():
await uwu()
async def uwu():
for i in guild.members:
print(i)
bot.run(LmaoWhyYouTrynaStealMyToken>:c)
I made a Welcomer bot using discord.py. It starts successfully, but does not send the specified message to the channel. Why is this?
import discord
from discord.ext import commands
import asyncio
import datetime
bot = commands.Bot(command_prefix="/")
#bot.event
async def on_member_join(member):
emb = discord.Embed(
color = 0xff0000,
title = "{member.name}, welcome to my server!",
description = f"Total participants: {len(list(member.guild.members))}",
timestamp = datetime.datetime.utcnow()
)
channel = bot.get_channel(CHANNEL ID)
await channel.send(embed = emb)
bot.run ("BOT TOKEN")
Your code has no issue in it. I suspect the intents are turned off. Go to Discord Developer Portal, and turn on Server Members Intent in your bot application.
on_member_join event doesn't execute without this intent turned off.
Edit:
Also try defining the intents in the code:
import discord
#....
from discord import Intents
intents = Intents.default()
intents.members = True
bot = commands.Bot(command_prefix="/", intents = intents)
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)