Reputation: 15
So I have his discord bot that I coded, and am trying to create a member count (while writing the online and offline members separately). I saw that I need Intents for this, so I enabled them in the developer portal and added this to my code:
intents = discord.Intents(members=True)
And later on, when the actual code using the intents is ran, added this:
client = commands.Bot(members_prefixes, intents=intents)
(members_prefixes being the prefix)
Every time I try to run this command, I get the exception:
Ignoring exception in command member_count: Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "main.py", line 190, in member_count
members = server.fetch_members()
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/guild.py", line 1367, in fetch_members
raise ClientException('Intents.members must be enabled to use this.')
discord.errors.ClientException: Intents.members must be enabled to use this.
Here is the beginning of my code and the function where the intents are used:
# bot.py
import asyncio
import json
import math
import os
import random
import requests as rq
import time
import re
import itertools
import discord
from discord.ext import commands
from numpy.core import long
from replit import db
import keepOn as ko
from flask import Flask
from threading import Thread
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix='-', intents=intents)
TOKEN = 'token'
@client.command(aliases=["mc"])
async def member_count(ctx):
embed = discord.Embed(
colour=discord.Colour.blue()
)
'''
client = commands.Bot(members_prefixes, intents=intents)
'''
intents.members = True
server = ctx.message.guild
members = server.fetch_members()
icon = ctx.message.guild.icon_url
embed.set_author(name="Number of members in " + server.name)
count = 0
async for member in ctx.message.guild.fetch_members(limit=5):
count+=1
count_online = 0
count_offline = 0
for member in members:
count += 1
for member in members:
if member.status == discord.Status.online:
count_online += 1
else:
count_offline += 1
embed.set_thumbnail(url=icon)
embed.add_field(name='Number of online members', value=str(count_online), inline=True)
embed.add_field(name='Number of offline members', value=str(count_offline), inline=True)
embed.add_field(name='Total number of members in the server', value=str(count), inline=True)
await ctx.send(embed=embed)
client.run(TOKEN)
Upvotes: 1
Views: 1650
Reputation: 15
Okay, so I figured out what was the problem. When I created the bot, I created a client with
client = commands.Bot()
At the time I didn't have the intents on in the code, and didn't have the intents enabled in my client code line.
All the answers on the internet instruct you to write the bot creation line, where you should enable the intents. So, I wrote that line in my code, absolutely forgetting that I've already created the bot object, and I just needed to enable the intents in the line itself.
client = commands.Bot(intents=intents)
Upvotes: 0
Reputation: 961
Try to enable all intents on Discord Developer website and use this code below. I had problem with this as well but it worked:
intents = discord.Intents.all()
bot = commands.Bot(command_prefix='!', intents=intents)
Don't worry about commnad_prefix
. You can remove that parameter.
Upvotes: 1