I'm trying to get all online members (with Bots) via this Code:
#client.command() async def printstats(ctx):
#define the variables
oc = 0
for user in ctx.guild.members:
if user.status != discord.Status.offline:
oc+=1
However, all as I get a result, is the number 1, even though there are 200 members online.
I already searched through the whole internet amd couldn't find a solution that worked. Could you please help me?
You have this issue because your bot is not using the presence intent, hence the bot can only see itself as online, and no other users, hence the value of 1. To fix this, you need to:
Enable the presence intent and server members in the developer portal
Then make sure to enable this intent in your code like so:
intents = discord.Intents()
intents.presences, intents.members = True, True
bot = commands.Bot(command_prefix="?", intents=intents) # whatever your prefix is etc, or client = discord.Client(intents=intents)
Related
I have been using autocode.com to create some Discord bots. I have very little programming experience and found autocode to be quite easy. However, I've tried asking a question on autocode's discord that no one seems to understand or is asking.
I am trying to create a bot that replies to reactions--but does not assign roles, but instead, provides a reply--either in thread or a DM to that user who uses that specific emoji reaction.
For example, this is what I am looking to do: if there is a bot message in #channelx, userX will react to that message with a pepperoni emoji and then a pizza bot will reply back with a message either in thread or DM such as, "Hi #userx, your pizza topping has been recorded and will be ready for pickup in 15 minutes, please visit this link.com to track your order".
Autocode has a bot that can react to reactions and assign roles but I can't seem to reverse engineer it give a reply, rather than assign roles.
I appreciate any assistance. Thanks!
What does autocode use? Python or node.js? If python, you can do something like this:
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('message'):
await message.channel.send('hi')
If node.js, you can do something like this:
client.on('messageCreate', msg => {
if (msg.content === 'specific message') {
msg.reply(`response text`);
}
});
I was previously a Community Hero at the Autocode Discord server. Try finding another app through this, and if none are available, the thing to do would be looking through the API's. Here's one for messaging in general, here's one for responding, and here's one for dm-ing.
Let's say, for example, I'd be making it reply to a reaction through DM:
The first thing you do is make sure the event trigger is set to message.reaction.add. This is so that the code you will write is triggered whenever a reaction is added.
Make either a switch or if statement to change what happens depending on what reaction triggers the code. In this example, I'll just use an if statement for easy explanation.
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
if (context.params.event.emoji.id == '1234567890') {
await lib.discord.users['#0.2.1'].dms.create({
recipient_id: `${context.params.event.member.user.id}`,
content: `Hi <#${context.params.event.member.user.id}>, your pizza topping has been recorded and will be ready for pickup in 15 minutes, please visit this link.com to track your order`
});
}
What this does is check if the thing that triggered this event has the emoji id equaling '1234567890': If it does, then it follows into that if statement, and if it does not, it skips over it.
In the future, please stay patient in the Autocode Discord server; The ones who are helping are also community members, similar to here. You may always ask the same question every now and then.
#client.event
async def on_ready(member):
if member.invites.uses == 2:
Premium = discord.utils.get(member.server.roles, name="Premium")
await client.add_roles(member, Premium)
I haven't had any errors and yes, intents are enabled
on_ready() will only execute on bot startup. What you want is probably on_member_join(member).
You can't get the invite by doing member.invite, as discord.Member objects does not provide it.
Member.add_roles() takes a list of roles, not a single role.
A solution to your problem could be to register each invite in a dict, then compare each of them when a member joins.
I just began trying to learn how to write my first Discord bot this morning so I am very inexperienced with discord.js, but I am familiar with JavaScript. However I have been searching for a couple of hours trying to find a way to call a function whenever a user in my server receives or loses a role.
In my server I have added the Patreon bot which assigns a role to users who become patrons. And I would like to create a custom bot that posts "hooray username" in my general channel when a user receive the patron role.
I can not find any example that shows how to detect when a user gains or loses a role. Is it possible to do this simply using an event? Or would I possibly need to periodically iterate over all users and maintain a list of their current roles while checking for changes?
I apologize that my question doesn't include any code or examples but I haven't made any progress and am reaching out to the SO community for guidance.
You want to use the event guildMemberUpdate.
You can compare the oldMember state to the newMember state and see what roles have changed.
This is not the most elegent solution but will get the job done.
client.on('guildMemberUpdate', (oldMember, newMember) => {
// Roles
const oldRoles = oldMember.roles.cache,
newRoles = newMember.roles.cache;
// Has Role?
const oldHas = oldRoles.has('role-id'),
newHas = newRoles.has('role-id');
// Check if removed or added
if (oldHas && !newHas) {
// Role has been removed
} else if (!oldHas && newHas) {
// Role has been added
}
});
https://discord.js.org/#/docs/main/stable/class/Client?scrollTo=e-guildMemberUpdate
This is simple with Client#guildMemberUpdate. Here’s some simple code that may help you
(This is the shortest code I could come up with)
client.on('guildMemberUpdate', async (oldMember, newMember) => {
if(oldMember.roles.cache.has('patreonRoleId')) return;
if(newMember.roles.cache.has('patreonRoleId')) {
//code here to run if member received role
//Warning: I didn’t test it as I had no time.
}
})
To see the removed role, just put the logical NOT operator (!) in front of both of the if statements like this:
if(!oldMember.roles.cache.has('patreonRoleId'))
if(!newMember.roles.cache.has('patreonRoleId'))
Note: make sure you have guildMembers intent enabled from the developers portal
I have been trying to make a bot using python for discord. This bot adds the member to another room depending in the room s/he is in. When I tried to run the code I keep getting this error
AttributeError: 'NoneType' object has no attribute 'roles'
This is the definition where I get the error
def get_sibling_role(member):
roles = member.roles; ret = None #<==there is an issue on this
for role in roles:
if role.name == "Brothers Waiting Room":
ret = ("Brother", role); break
elif role.name == "Sisters Waiting Room":
ret = ("Sister", role); break
return ret
#I already have member defined which returns the member ID
Can anyone help me find out the issue in my definition?
The obvious reason is that value of member is None. To avoid that you can add a check for "not None". I am also assuming that the misalignment is because of posting the code in question rather than in the code.
def get_sibling_role(member):
if member is None:
return None
roles = member.roles; ret = None #<==there is an issue on this
for role in roles:
if role.name == "Brothers Waiting Room":
ret = ("Brother", role); break
elif role.name == "Sisters Waiting Room":
ret = ("Sister", role); break
return ret
The NoneType issue is an error where many programmers face discord.py for a few main reasons
1- My own mistake in the previous problem was outside the given code. The code that I wrote was correct, however, I had two client instances defined in the same code, which made the whole program get confused, which returned none for members and for roles, so modify your discord.Client or commands.Bot constructor.
client = discord.Client(intents=intents)
if you want to test and debug, I would advise printing the first member in the server by using the following code under the on_ready(): function.
print (guild.members[1])
2- The second common mistake is that people tend to not use intents, they have to use intents to get members
https://discordpy.readthedocs.io/en/latest/intents.html
also, you should enable them (Privileged Gateway) as explained in the previous doc in the discord portal https://discord.com/developers/applications
3- Update your discord.py to version 1.5.0. or higher. Make sure you're installing it to the correct environment
I need to set users permissions, i tried
chanel2.overwritePermissions(message.member,{'SEND_MESSAGES': true,'READ_MESSAGES': true})
but it overwrited all permissions (for everyone not just message.member), not just add specific permissions for message.member (but for the 1 member, it does what i need)
Its best if you refer to this guide for all of the information you need to know about setting user permissions in a channel. It looks like it is erroring because message.member is not grabbing the user id to set in the overwritePermissions. Ill explain it all here, but next time try adding some error information and context to your question and code :)
You need to make sure that you have a channel object, so you can grab that like so:
const channelID = message.channel.id //alternatively you can hardcode one like so: "588868740980932620"
const channel = message.guild.channels.find(c => {return c.id === channelID})
After you grab the channel object, you can now set the channel permissions like so:
// channel is whatever your constant variable is in the section above
// message.member.user.id is identical to message.author.id
channel.overwritePermissions(message.member.user.id, {
SEND_MESSAGES: true,
READ_MESSAGES: true,
ATTACH_FILES: true
});
You can find permission flags here.
Hopefully, this helps you. DiscordJS Guide and Discord JS Docs are very helpful areas to look for help.