Add RequestContext.SendMessage + Silent messages

This commit is contained in:
Basique Evangelist 2019-12-08 20:34:03 +00:00
parent b3dbdb5806
commit 000a0d8718
Signed by untrusted user: BasiqueEvangelist
GPG Key ID: B370219149301706
8 changed files with 37 additions and 73 deletions

View File

@ -28,13 +28,13 @@ namespace JetKarmaBot.Commands
{
if (recipientId != default(int))
{
await ctx.Client.SendTextMessageAsync(ctx.EventArgs.Message.Chat.Id, currentLocale["jetkarmabot.award.errdup"]);
await ctx.SendMessage(currentLocale["jetkarmabot.award.errdup"]);
return false;
}
recipientId = await db.Users.Where(x => x.Username == arg).Select(x => x.UserId).FirstOrDefaultAsync();
if (recipientId == default(int))
{
await ctx.Client.SendTextMessageAsync(ctx.EventArgs.Message.Chat.Id, currentLocale["jetkarmabot.award.errbadusername"]);
await ctx.SendMessage(currentLocale["jetkarmabot.award.errbadusername"]);
return false;
}
}
@ -44,7 +44,7 @@ namespace JetKarmaBot.Commands
awardTypeText = arg;
else
{
await ctx.Client.SendTextMessageAsync(ctx.EventArgs.Message.Chat.Id, currentLocale["jetkarmabot.award.errdup"]);
await ctx.SendMessage(currentLocale["jetkarmabot.award.errdup"]);
return false;
}
}
@ -57,7 +57,7 @@ namespace JetKarmaBot.Commands
if (recipientId == default(int))
{
await ctx.Client.SendTextMessageAsync(ctx.EventArgs.Message.Chat.Id, currentLocale["jetkarmabot.award.errawardnoreply"]);
await ctx.SendMessage(currentLocale["jetkarmabot.award.errawardnoreply"]);
return false;
}
@ -66,21 +66,15 @@ namespace JetKarmaBot.Commands
if (awarder.Id == recipientId)
{
await ctx.Client.SendTextMessageAsync(
ctx.EventArgs.Message.Chat.Id,
currentLocale["jetkarmabot.award.errawardself"],
replyToMessageId: ctx.EventArgs.Message.MessageId);
await ctx.SendMessage(currentLocale["jetkarmabot.award.errawardself"]);
return false;
}
if (ctx.GetFeature<ChatCommandRouter.Feature>().Router.Me.Id == recipientId)
{
await ctx.Client.SendTextMessageAsync(
ctx.EventArgs.Message.Chat.Id,
awarding
await ctx.SendMessage(awarding
? currentLocale["jetkarmabot.award.errawardbot"]
: currentLocale["jetkarmabot.award.errrevokebot"],
replyToMessageId: ctx.EventArgs.Message.MessageId);
: currentLocale["jetkarmabot.award.errrevokebot"]);
return false;
}
@ -113,10 +107,7 @@ namespace JetKarmaBot.Commands
var response = message + "\n" + String.Format(currentLocale["jetkarmabot.award.statustext"], recUserName, prevCount + (awarding ? 1 : -1), awardType.Symbol);
await ctx.Client.SendTextMessageAsync(
ctx.EventArgs.Message.Chat.Id,
response,
replyToMessageId: ctx.EventArgs.Message.MessageId);
await ctx.SendMessage(response);
return true;
}

View File

@ -21,27 +21,18 @@ namespace JetKarmaBot.Commands
var currentLocale = Locale[(await db.Chats.FindAsync(ctx.EventArgs.Message.Chat.Id)).Locale];
if (cmd.Parameters.Length < 1)
{
await ctx.Client.SendTextMessageAsync(
args.Message.Chat.Id,
currentLocale["jetkarmabot.changelocale.getlocale"],
replyToMessageId: args.Message.MessageId);
await ctx.SendMessage(currentLocale["jetkarmabot.changelocale.getlocale"]);
return false;
}
else if (cmd.Parameters[0] == "list")
{
await ctx.Client.SendTextMessageAsync(
args.Message.Chat.Id,
currentLocale["jetkarmabot.changelocale.listalltext"] + "\n"
+ string.Join("\n", Locale.Select(a => a.Key)),
replyToMessageId: args.Message.MessageId);
await ctx.SendMessage(currentLocale["jetkarmabot.changelocale.listalltext"] + "\n"
+ string.Join("\n", Locale.Select(a => a.Key)));
return false;
}
else if (cmd.Parameters[0] == "all")
{
await ctx.Client.SendTextMessageAsync(
args.Message.Chat.Id,
currentLocale["jetkarmabot.changelocale.errorall"],
replyToMessageId: args.Message.MessageId);
await ctx.SendMessage(currentLocale["jetkarmabot.changelocale.errorall"]);
return false;
}
string localeId;
@ -54,10 +45,9 @@ namespace JetKarmaBot.Commands
}
catch (LocalizationException e)
{
await ctx.Client.SendTextMessageAsync(
args.Message.Chat.Id,
currentLocale["jetkarmabot.changelocale.toomany"] + "\n" + string.Join("\n", (e.Data["LocaleNames"] as Locale[]).Select(x => x.Name)),
replyToMessageId: args.Message.MessageId);
await ctx.SendMessage(
currentLocale["jetkarmabot.changelocale.toomany"] + "\n"
+ string.Join("\n", (e.Data["LocaleNames"] as Locale[]).Select(x => x.Name)));
return false;
}
(await db.Chats.FindAsync(args.Message.Chat.Id)).Locale = localeId;
@ -65,11 +55,9 @@ namespace JetKarmaBot.Commands
currentLocale = Locale[localeId];
await ctx.Client.SendTextMessageAsync(
args.Message.Chat.Id,
(currentLocale.HasNote ? currentLocale["jetkarmabot.changelocale.beforenote"] + currentLocale.Note + "\n" : "")
+ currentLocale["jetkarmabot.changelocale.justchanged"],
replyToMessageId: args.Message.MessageId);
await ctx.SendMessage(
(currentLocale.HasNote ? currentLocale["jetkarmabot.changelocale.beforenote"] + currentLocale.Note + "\n" : "")
+ currentLocale["jetkarmabot.changelocale.justchanged"]);
return true;
}

View File

@ -23,13 +23,10 @@ namespace JetKarmaBot.Commands
{
var db = ctx.Database;
var currentLocale = Locale[(await db.Chats.FindAsync(ctx.EventArgs.Message.Chat.Id)).Locale];
string resp = currentLocale["jetkarmabot.currencies.listtext"] + "\n" + string.Join("\n",
(await db.AwardTypes.ToListAsync()).Select(x => $"{x.Symbol} ({x.CommandName}) <i>{currentLocale["jetkarmabot.awardtypes.nominative." + x.CommandName]}</i>"));
await ctx.Client.SendTextMessageAsync(
ctx.EventArgs.Message.Chat.Id,
resp,
replyToMessageId: ctx.EventArgs.Message.MessageId,
parseMode: ParseMode.Html);
await ctx.SendMessage(
currentLocale["jetkarmabot.currencies.listtext"] + "\n" + string.Join("\n",
(await db.AwardTypes.ToListAsync())
.Select(x => $"{x.Symbol} ({x.CommandName}) <i>{currentLocale["jetkarmabot.awardtypes.nominative." + x.CommandName]}</i>")));
return true;
}
}

View File

@ -31,20 +31,12 @@ namespace JetKarmaBot.Commands
var router = ctx.GetFeature<ChatCommandRouter.Feature>().Router;
if (ctx.Command.Parameters.Length < 1)
{
await ctx.Client.SendTextMessageAsync(
ctx.EventArgs.Message.Chat.Id,
router.GetHelpText(currentLocale),
replyToMessageId: ctx.EventArgs.Message.MessageId,
parseMode: ParseMode.Html);
await ctx.SendMessage(router.GetHelpText(currentLocale));
return true;
}
else
{
await ctx.Client.SendTextMessageAsync(
ctx.EventArgs.Message.Chat.Id,
router.GetHelpTextFor(ctx.Command.Parameters[0], currentLocale),
replyToMessageId: ctx.EventArgs.Message.MessageId,
parseMode: ParseMode.Html);
await ctx.SendMessage(router.GetHelpTextFor(ctx.Command.Parameters[0], currentLocale));
return true;
}
}

View File

@ -18,8 +18,6 @@ namespace JetKarmaBot.Commands
var asker = ctx.EventArgs.Message.From;
var awardTypeName = ctx.Command.Parameters.FirstOrDefault();
string response;
if (string.IsNullOrWhiteSpace(awardTypeName))
awardTypeName = "star";
@ -29,7 +27,7 @@ namespace JetKarmaBot.Commands
var awardTypeId = await awardTypeIdQuery.FirstAsync();
var awardType = await db.AwardTypes.FindAsync(awardTypeId);
response = string.Format(currentLocale["jetkarmabot.leaderboard.specifictext"], awardType.Symbol) + "\n" + string.Join('\n',
await ctx.SendMessage(string.Format(currentLocale["jetkarmabot.leaderboard.specifictext"], awardType.Symbol) + "\n" + string.Join('\n',
await Task.WhenAll((await db.Awards
.Where(x => x.ChatId == ctx.EventArgs.Message.Chat.Id && x.AwardTypeId == awardTypeId)
.GroupBy(x => x.ToId)
@ -38,12 +36,7 @@ namespace JetKarmaBot.Commands
.Take(5)
.ToListAsync())
.Select(async (x, index) => $"{index + 1}. {(await db.Users.FindAsync(x.UserId)).Username} - {x.Amount}"))
);
await ctx.Client.SendTextMessageAsync(
ctx.EventArgs.Message.Chat.Id,
response,
replyToMessageId: ctx.EventArgs.Message.MessageId);
));
return true;
}

View File

@ -60,10 +60,7 @@ namespace JetKarmaBot.Commands
.SumAsync(x => x.Amount), awardType.Symbol);
}
await ctx.Client.SendTextMessageAsync(
ctx.EventArgs.Message.Chat.Id,
response,
replyToMessageId: ctx.EventArgs.Message.MessageId);
await ctx.SendMessage(response);
return true;
}

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using JetKarmaBot.Commands;
using JetKarmaBot.Models;
using Telegram.Bot;
@ -24,5 +25,13 @@ namespace JetKarmaBot.Services.Handling
}
public object GetService(Type serviceType) => Features.First(x => x.GetType() == serviceType);
public T GetFeature<T>() => (T)Features.First(x => x is T);
//Method to reduce WET in commands
public Task SendMessage(string text) => Client.SendTextMessageAsync(
EventArgs.Message.Chat.Id,
text,
replyToMessageId: EventArgs.Message.MessageId,
disableNotification: true,
parseMode: Telegram.Bot.Types.Enums.ParseMode.Html);
}
}

View File

@ -81,10 +81,7 @@ namespace JetKarmaBot.Services.Handling
if (!TimeoutCache[uid].TimeoutMessaged)
{
Locale currentLocale = Locale[(await ctx.Database.Chats.FindAsync(ctx.EventArgs.Message.Chat.Id)).Locale];
await ctx.Client.SendTextMessageAsync(
ctx.EventArgs.Message.Chat.Id,
currentLocale["jetkarmabot.ratelimit"],
replyToMessageId: ctx.EventArgs.Message.MessageId);
await ctx.SendMessage(currentLocale["jetkarmabot.ratelimit"]);
TimeoutCache[uid] = new TimeoutStats() { TimeoutMessaged = true, CooldownDate = TimeoutCache[uid].CooldownDate };
}
return;