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)
Related
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.
This question already has an answer here:
Commands don't run in discord.py 2.0 - no errors, but run in discord.py 1.7.3
(1 answer)
Closed 6 months ago.
I am currently making a discord bot. I pasted some codes that I previously wrote that works fine last time, but it is not working now.
#LINK FILES
import bot_token
import task
from discord.ext import tasks, commands
#LIBRARIES
import discord
import os
#VARIABLES
token = bot_token.token
client = discord.Client()
#CODE
#client.event
async def on_ready():
print('We have log in as {0.user}'. format(client))
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('!hi'):
await message.channel.send('hello')
client. Run(token)
When I run the code, it shows that the bot is online, but when I send !hi in the server, the bot has no reaction. May I know whats wrong with my code?
Does your bot have the required permissions and intents to read messages?
Permissions need to be set in the discord developer portal and need to be agreed on by the server owner when the bot is added to a server. This specifies what your bot is allowed to do.
Intents specify what information you want the bot to receive from discord. You can try:
intents = discord.Intents(messages=True)
client = discord.Client(intents=intents)
or
intents = discord.Intents.all()
client = discord.Client(intents=intents)
If either permissions or intents are missing, your bot will not receive any messages and thus not react to them.
You need to use the message_content intent.
Make sure you have it enabled in your bot's application page.
intents = discord.Intents(message_content=True)
client = discord.Client(intents=intents)
Im using the discord.py library and using replit for the bot, but my code doesnt work.
my code:
import os
import discord
from discord.utils import get
client = discord.Client()
#client.event
async def on_ready():
print ("We have logged in as {0.user}".format(client))
#client.event
async def on_message(message):
if message.content == ";":
member = message.author
role = get(member.guild.roles, name="Recruiter")
await member.add_roles(role)
client.run(os.getenv("TOKEN"))
First of all, you're using client object and it is not a bot.
This is the way to create a proper bot (not client):
from discord.ext import commands
bot = commands.Bot(command_prefix="!")
You can't code reactions roles without reading the discord.py docs. It involves lots of stuff.
If you want to code it yourself, the only way is to learn and do.
I still have not figured out how to add/remove roles on the bot itself, without executing commands in the chat. It has to be silently in the background. Can someone please help me.
Current code below that does work but not the way I want it to. I do not want the bot to type a command to give itself a role, I need it to automatically give it silently without any chat messages being executed. My ideal idea is to execute the addRole function in the on_ready event somehow, without sending message to get a role.
import aiohttp
from datetime import datetime
from dotenv import load_dotenv
from discord.ext import commands
import discord
import os
load_dotenv()
DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
bot = commands.Bot(command_prefix="$")
bot.remove_command('help')
#bot.event
async def on_ready():
await bot.wait_until_ready()
bot.session = aiohttp.ClientSession()
await bot.get_channel(402353715718277809).send("$addRole")
#bot.command()
async def addRole(ctx):
role = discord.utils.get(ctx.guild.roles, name="market_green")
member = ctx.guild.get_member(bot.user.id)
await member.add_roles(role)
bot.run(DISCORD_TOKEN)
The guild object in discord.py has a .me attribute (docs) which represents yourself in that guild.
So on start you can get that guild based on it's id (in the on_ready event) and assign yourself a role with add_roles, example code(with guildid the id of the guild you want the role in):
guild = bot.get_guild(guildid)
role = discord.utils.get(guild.roles, name="market_green")
await guild.me.add_roles(role)
can someone tell my why my code is not working?
i want the bot to message me what i type after !test.
import discord
from discord.ext.commands import Bot
from discord.ext import commands
from discord import Game
command_prefix = "!"
client = discord.Client()
bot = commands.Bot(command_prefix='!')
#client.event
async def on_ready():
print('prihlaseno za {0.user}'.format(client))
#bot.command()
async def test(ctx, *, mess):
await ctx.send(mess)
client.run('token')
You cannot mix client with bot. Use one or the other (bot is typically used). Then change your code to fit one of these. For example, if you pick bot, rename #client.event to bot.event and change client.run to bot.run.
You are also needlessly importing the Bot class again when you have already imported it from discord.ext.commands. I'm also not sure what discord.Game is at the end of your imports.