GuildMemberAdded not working

import { Events, GuildMember, TextChannel } from "discord.js";
import Event from "../../classes/Event";
import OopsieClient from "../../classes/OopsieClient";
import { prisma } from "../../lib/prisma";

export default class GuildMemberAdd extends Event {
constructor(client: OopsieClient) {
super(client, { name: Events.GuildMemberAdd, description: "Guild Member Added Event", once: true });
}

async execute(member: GuildMember) {
await prisma.user.create({
data: {
name: member.user.username,
level: 1,
tokens: 100,
},
});
}
}
import { Events, GuildMember, TextChannel } from "discord.js";
import Event from "../../classes/Event";
import OopsieClient from "../../classes/OopsieClient";
import { prisma } from "../../lib/prisma";

export default class GuildMemberAdd extends Event {
constructor(client: OopsieClient) {
super(client, { name: Events.GuildMemberAdd, description: "Guild Member Added Event", once: true });
}

async execute(member: GuildMember) {
await prisma.user.create({
data: {
name: member.user.username,
level: 1,
tokens: 100,
},
});
}
}
whenever I ask someone to join, it doesnt run and I cannot trace why
16 Replies
d.js toolkit
d.js toolkit2mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
monbrey
monbrey2mo ago
Why is this once? Does your client have the GuildMembers intent?
jei
jeiOP2mo ago
cuz its only supposed to run once, is that the incorrect behaviour yes
import dotenv from "dotenv";
import { Client, Collection, Events, GatewayIntentBits } from "discord.js";

import Handler from "./Handler";
import { IClient } from "../interfaces";
import Command from "./Command";
import SubCommand from "./SubCommand";
import EventEmitter from "events";
import { prisma } from "../lib/prisma";

dotenv.config();

export default class OopsieClient extends Client implements IClient {
handler: Handler;
commands: Collection<string, Command>;
subCommands: Collection<string, SubCommand>;
cooldowns: Collection<string, Collection<string, number>>;

constructor() {
super({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers] });

this.handler = new Handler(this);
this.commands = new Collection();
this.subCommands = new Collection();
this.cooldowns = new Collection();
}
init(): void {
this.loadHandlers();
this.login(process.env.DISCORD_TOKEN)
.then(() => console.log("Logged In"))
.catch((err) => console.error(err));
}

loadHandlers(): void {
this.handler.loadEvents();
this.handler.loadCommands();
}
}
import dotenv from "dotenv";
import { Client, Collection, Events, GatewayIntentBits } from "discord.js";

import Handler from "./Handler";
import { IClient } from "../interfaces";
import Command from "./Command";
import SubCommand from "./SubCommand";
import EventEmitter from "events";
import { prisma } from "../lib/prisma";

dotenv.config();

export default class OopsieClient extends Client implements IClient {
handler: Handler;
commands: Collection<string, Command>;
subCommands: Collection<string, SubCommand>;
cooldowns: Collection<string, Collection<string, number>>;

constructor() {
super({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers] });

this.handler = new Handler(this);
this.commands = new Collection();
this.subCommands = new Collection();
this.cooldowns = new Collection();
}
init(): void {
this.loadHandlers();
this.login(process.env.DISCORD_TOKEN)
.then(() => console.log("Logged In"))
.catch((err) => console.error(err));
}

loadHandlers(): void {
this.handler.loadEvents();
this.handler.loadCommands();
}
}
Amgelo
Amgelo2mo ago
it'll only run in the first user that joins though then the listener would be unregistered, so further joins wouldn't call that pretty sure that's not what you want you don't want to run the event only once, you want it to run once per user you could either code that or change your db logic to use an upsert which is basically a "create, but if it exists update with this" call you wouldn't update with anything though so just pass nothing on that check prisma's docs on upsert
jei
jeiOP2mo ago
for each command there's a once param so ill tr changing that first cuz the create logic will only push new data to the database, while im here is it possible to check if someone bought a SKU?
Amgelo
Amgelo2mo ago
for each command there's a once param
we get that, and we're assuming it behaves like EventEmitter#once(), which unregisters the listener after it's called once
the create logic will only push new data
you don't want to create the same member twice though, that's why I recommended an upsert, which will first try to find if there's a row that matches some data
jei
jeiOP2mo ago
are we assuming case of "Leave & Rejoining"?
Amgelo
Amgelo2mo ago
on my second answer yes I'm assuming you have an autoincremented id field
jei
jeiOP2mo ago
nope, its cuid
Amgelo
Amgelo2mo ago
which would be your primary key well, I mostly meant about it being autogenerated
jei
jeiOP2mo ago
ok, yea
Amgelo
Amgelo2mo ago
the point I'm trying to make is that duplicate records are possible in that setup if they rejoin
jei
jeiOP2mo ago
assuming that GuildMemberRemove detects when a user is removed ffrom the server, I handle deleting the data
Amgelo
Amgelo2mo ago
ah, that should solve it then your only issue would be that once in that case
jei
jeiOP2mo ago
ok, i just thought about this before making the change, so If i run my bot, it still doesnt detect a user joining, I can run a sim join, but whenever i have a friend join it still doesnt detect I can double check without the once, but its seems like weird behavior
Amgelo
Amgelo2mo ago
it should only run for the first join it detects which is exactly what once does or well EventEmitter#once() if your once works in some other way we don't have any way to tell, but using EventEmitter's would be the reasonable way
Want results from more Discord servers?
Add your server