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)