fix: Recreate Discord client instance when switching tokens to ensure clean state
This commit is contained in:
parent
8d1040ea73
commit
8d884428e4
|
@ -2,13 +2,16 @@ import { DiscordLoginSchema } from '../schemas.js';
|
||||||
import { ToolHandler } from './types.js';
|
import { ToolHandler } from './types.js';
|
||||||
import { handleDiscordError } from "../errorHandler.js";
|
import { handleDiscordError } from "../errorHandler.js";
|
||||||
import { info, error } from '../logger.js';
|
import { info, error } from '../logger.js';
|
||||||
|
import { Client, GatewayIntentBits } from 'discord.js';
|
||||||
|
import { createToolContext } from './tools.js';
|
||||||
|
|
||||||
export const loginHandler: ToolHandler = async (args, { client }) => {
|
export const loginHandler: ToolHandler = async (args, context) => {
|
||||||
DiscordLoginSchema.parse(args);
|
DiscordLoginSchema.parse(args);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Check if token is provided in the request
|
// Check if token is provided in the request
|
||||||
const token = args.token;
|
const token = args.token;
|
||||||
|
let { client } = context;
|
||||||
|
|
||||||
// Log initial client state
|
// Log initial client state
|
||||||
info(`Login handler called with client state: ${JSON.stringify({
|
info(`Login handler called with client state: ${JSON.stringify({
|
||||||
|
@ -30,16 +33,35 @@ export const loginHandler: ToolHandler = async (args, { client }) => {
|
||||||
await client.destroy();
|
await client.destroy();
|
||||||
info('Client destroyed successfully');
|
info('Client destroyed successfully');
|
||||||
|
|
||||||
// Set the new token and ensure it's not null
|
// Create a new client instead of reusing the old one
|
||||||
|
info('Creating new client instance for new token');
|
||||||
|
const newClient = new Client({
|
||||||
|
intents: [
|
||||||
|
GatewayIntentBits.Guilds,
|
||||||
|
GatewayIntentBits.GuildMessages,
|
||||||
|
GatewayIntentBits.MessageContent,
|
||||||
|
GatewayIntentBits.GuildMessageReactions
|
||||||
|
]
|
||||||
|
});
|
||||||
|
|
||||||
|
// Replace the old client in the context
|
||||||
|
Object.assign(context, { client: newClient });
|
||||||
|
client = newClient;
|
||||||
|
|
||||||
|
// Set the token on the new client
|
||||||
client.token = token;
|
client.token = token;
|
||||||
info(`Token set: ${!!client.token}`);
|
info(`Token set on new client: ${!!client.token}`);
|
||||||
|
|
||||||
// Login with the new token
|
// Login with the new token
|
||||||
info('Attempting login with new token');
|
info('Attempting login with new token on new client');
|
||||||
try {
|
try {
|
||||||
await client.login(token);
|
await client.login(token);
|
||||||
info(`Login successful, new client user: ${client.user?.tag}`);
|
info(`Login successful, new client user: ${client.user?.tag}`);
|
||||||
|
|
||||||
|
if (!client.isReady()) {
|
||||||
|
throw new Error('Client login completed but client is not in ready state');
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
content: [{ type: "text", text: `Successfully switched from ${currentBotTag} to ${client.user?.tag}` }]
|
content: [{ type: "text", text: `Successfully switched from ${currentBotTag} to ${client.user?.tag}` }]
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue