Skip to content

Commit

Permalink
Add command to promote user to admin
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrijs-pavlovs-dev committed Dec 18, 2023
1 parent 08779ca commit eb5fbad
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Preview this bot in telegram https://t.me/WhaleTestAppBot
- `/adminhello <telegram_id> <text>` Command: Admins can send personalized messages to users by invoking this command followed by the user's Telegram ID and the intended message.
- `/getuserid <username>` Admins can retrivies any telegram user's id by providing username
- User Persistence: The bot stores user information in a PostgresSQL database, making it accessible for future interactions and additional functionality.
- `/makeadmin <userid>` Admins can make other users an admin

### Project Structure

Expand Down
54 changes: 51 additions & 3 deletions apps/backend/src/services/bot.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,15 @@ export class BotService {
}
if ('text' in ctx.message) {
const args = ctx.message.text.split(' ').slice(1);
const username = args[0]; // Assuming this is the Telegram username
const username = args[0];
console.log('Username to find:', username);

// Ensure that username is provided
if (!username) {
await ctx.reply('Please provide a username.');
return;
}

const stringSession = ''; // Your saved session string
const stringSession = '';
const botToken = this.configService.get<string>('TELEGRAM_BOT_TOKEN');
const appId = this.configService.get<number>('TELEGRAM_APP_ID');
const appIdHash = this.configService.get<string>('TELEGRAM_APP_ID_HASH');
Expand All @@ -112,4 +111,53 @@ export class BotService {
await ctx.reply('This command requires a text message.');
}
}
@Command('makeadmin')
async makeUserAdmin(@Ctx() ctx: Context) {
const adminUser = await this.userRepository.findOne({ where: { id: ctx.from.id.toString() } });
// Check if the user invoking the command is an admin
if (!adminUser || !adminUser.is_admin) {
await ctx.reply('You are not authorized to use this command.');
return;
}

if ('text' in ctx.message) {
const args = ctx.message.text.split(' ').slice(1);
const adminId = args[0];

if (!adminId) {
await ctx.reply('Please provide an ID.');
return;
}

let userToMakeAdmin = await this.userRepository.findOne({ where: { id: adminId } });

// If user not found, create a new user
if (!userToMakeAdmin) {
try {
userToMakeAdmin = this.userRepository.create({
id: adminId,
is_bot: false,
first_name: '',
last_name: '',
username: '',
language_code: '',
is_premium: false,
added_to_attachment_menu: false,
is_admin: true, // Set as admin
});
await this.userRepository.save(userToMakeAdmin);
await ctx.reply(`New user created and set as admin with ID: ${adminId}`);
} catch (e) {
await ctx.reply(`Error adding new user: ` + e.message);
}
} else {
// Update the user's is_admin status
userToMakeAdmin.is_admin = true;
await this.userRepository.save(userToMakeAdmin);
await ctx.reply(`User with ID: ${adminId} is now an admin.`);
}
} else {
await ctx.reply('This command requires a text message.');
}
}
}

0 comments on commit eb5fbad

Please sign in to comment.