Skip to content

Commit 895a535

Browse files
Added /command@botname syntax to all commands
1 parent dbdf8f2 commit 895a535

File tree

4 files changed

+11
-3
lines changed

4 files changed

+11
-3
lines changed

NotifierBot/Sources/Notifier/Commands/Command.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ extension Command {
3232
}
3333

3434
var handler: Handler {
35-
CommandHandler(name: name, commands: commands, callback: { (update, context) in
35+
// Add 'Command@Botname' syntax
36+
let commands = self.commands + self.commands.map({ "\($0)@\(botUser.username!)" })
37+
return CommandHandler(name: name, commands: commands, callback: { (update, context) in
3638
do {
3739
// Check if the sender is a user
3840
guard let userID = update.message?.from?.id else {

NotifierBot/Sources/Notifier/Commands/GetPermissionsCommand.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ struct GetPermissionsCommand: Command {
3131
let username: String!
3232
if args.count == 0 {
3333
// Use the reply message
34-
guard let user = update.message?.replyToMessage?.from, !user.isBot else {
34+
guard let user = update.message?.replyToMessage?.from, user.username != botUser.username else {
3535
try bot.sendMessage("Error: Please respond to a message of a user.", to: chatID)
3636
return
3737
}

NotifierBot/Sources/Notifier/Commands/SetPermissionsCommand.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ struct SetPermissionsCommand: Command {
2727
let username: String!
2828
if args.count == 1 {
2929
// Use the reply message
30-
guard let user = update.message?.replyToMessage?.from, !user.isBot else {
30+
guard let user = update.message?.replyToMessage?.from, user.username != botUser.username else {
3131
try bot.sendMessage("Error: Please respond to a message of a user.", to: chatID)
3232
return
3333
}

NotifierBot/Sources/Notifier/main.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ let allCommands: [Command] = [
4848

4949
var token: String!
5050
let bot: Bot!
51+
let botUser: User!
5152

5253
do {
5354

@@ -64,6 +65,11 @@ do {
6465
exit(1)
6566
}
6667
bot = try Bot(token: token)
68+
botUser = try bot.getMe().wait()
69+
guard let _ = botUser.username else {
70+
print("Unable to retrieve bot username")
71+
exit(1)
72+
}
6773

6874
// Create a dispatcher and start polling
6975
let dispatcher = Dispatcher(bot: bot)

0 commit comments

Comments
 (0)