Using declarations and other code reformats

This commit is contained in:
Kwoth
2021-12-26 03:22:45 +01:00
parent d18f9429c6
commit b85ba177cd
64 changed files with 2349 additions and 2736 deletions

View File

@@ -24,11 +24,9 @@ public class ChatterBotSession : IChatterBotSession
public async Task<string> Think(string message)
{
using (var http = _httpFactory.CreateClient())
{
var res = await http.GetStringAsync(string.Format(ApiEndpoint, message)).ConfigureAwait(false);
var cbr = JsonConvert.DeserializeObject<ChatterBotResponse>(res);
return cbr.BotSay.Replace("<br/>", "\n", StringComparison.InvariantCulture);
}
using var http = _httpFactory.CreateClient();
var res = await http.GetStringAsync(string.Format(ApiEndpoint, message)).ConfigureAwait(false);
var cbr = JsonConvert.DeserializeObject<ChatterBotResponse>(res);
return cbr.BotSay.Replace("<br/>", "\n", StringComparison.InvariantCulture);
}
}

View File

@@ -21,22 +21,20 @@ public class OfficialCleverbotSession : IChatterBotSession
public async Task<string> Think(string input)
{
using (var http = _httpFactory.CreateClient())
using var http = _httpFactory.CreateClient();
var dataString = await http.GetStringAsync(string.Format(QueryString, input, _cs ?? "")).ConfigureAwait(false);
try
{
var dataString = await http.GetStringAsync(string.Format(QueryString, input, _cs ?? "")).ConfigureAwait(false);
try
{
var data = JsonConvert.DeserializeObject<CleverbotResponse>(dataString);
var data = JsonConvert.DeserializeObject<CleverbotResponse>(dataString);
_cs = data?.Cs;
return data?.Output;
}
catch
{
Log.Warning("Unexpected cleverbot response received: ");
Log.Warning(dataString);
return null;
}
_cs = data?.Cs;
return data?.Output;
}
catch
{
Log.Warning("Unexpected cleverbot response received: ");
Log.Warning(dataString);
return null;
}
}
}
@@ -62,41 +60,37 @@ public class CleverbotIOSession : IChatterBotSession
private async Task<string> GetNick()
{
using (var _http = _httpFactory.CreateClient())
using (var msg = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("user", _user),
new KeyValuePair<string, string>("key", _key),
}))
using (var data = await _http.PostAsync(_createEndpoint, msg).ConfigureAwait(false))
using var _http = _httpFactory.CreateClient();
using var msg = new FormUrlEncodedContent(new[]
{
var str = await data.Content.ReadAsStringAsync().ConfigureAwait(false);
var obj = JsonConvert.DeserializeObject<CleverbotIOCreateResponse>(str);
if (obj.Status != "success")
throw new OperationCanceledException(obj.Status);
new KeyValuePair<string, string>("user", _user),
new KeyValuePair<string, string>("key", _key),
});
using var data = await _http.PostAsync(_createEndpoint, msg).ConfigureAwait(false);
var str = await data.Content.ReadAsStringAsync().ConfigureAwait(false);
var obj = JsonConvert.DeserializeObject<CleverbotIOCreateResponse>(str);
if (obj.Status != "success")
throw new OperationCanceledException(obj.Status);
return obj.Nick;
}
return obj.Nick;
}
public async Task<string> Think(string input)
{
using (var _http = _httpFactory.CreateClient())
using (var msg = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("user", _user),
new KeyValuePair<string, string>("key", _key),
new KeyValuePair<string, string>("nick", await _nick),
new KeyValuePair<string, string>("text", input),
}))
using (var data = await _http.PostAsync(_askEndpoint, msg).ConfigureAwait(false))
using var _http = _httpFactory.CreateClient();
using var msg = new FormUrlEncodedContent(new[]
{
var str = await data.Content.ReadAsStringAsync().ConfigureAwait(false);
var obj = JsonConvert.DeserializeObject<CleverbotIOAskResponse>(str);
if (obj.Status != "success")
throw new OperationCanceledException(obj.Status);
new KeyValuePair<string, string>("user", _user),
new KeyValuePair<string, string>("key", _key),
new KeyValuePair<string, string>("nick", await _nick),
new KeyValuePair<string, string>("text", input),
});
using var data = await _http.PostAsync(_askEndpoint, msg).ConfigureAwait(false);
var str = await data.Content.ReadAsStringAsync().ConfigureAwait(false);
var obj = JsonConvert.DeserializeObject<CleverbotIOAskResponse>(str);
if (obj.Status != "success")
throw new OperationCanceledException(obj.Status);
return obj.Response;
}
return obj.Response;
}
}

View File

@@ -30,33 +30,31 @@ public class GirlRating
{
try
{
using (var img = Image.Load(_images.RategirlMatrix))
using var img = Image.Load(_images.RategirlMatrix);
const int minx = 35;
const int miny = 385;
const int length = 345;
var pointx = (int)(minx + (length * (Hot / 10)));
var pointy = (int)(miny - (length * ((Crazy - 4) / 6)));
using (var pointImg = Image.Load(_images.RategirlDot))
{
const int minx = 35;
const int miny = 385;
const int length = 345;
var pointx = (int)(minx + (length * (Hot / 10)));
var pointy = (int)(miny - (length * ((Crazy - 4) / 6)));
using (var pointImg = Image.Load(_images.RategirlDot))
{
img.Mutate(x => x.DrawImage(pointImg, new(pointx - 10, pointy - 10), new GraphicsOptions()));
}
var imgStream = new MemoryStream();
img.SaveAsPng(imgStream);
return imgStream;
//using (var byteContent = new ByteArrayContent(imgStream.ToArray()))
//{
// http.AddFakeHeaders();
// using (var reponse = await http.PutAsync("https://transfer.sh/img.png", byteContent).ConfigureAwait(false))
// {
// url = await reponse.Content.ReadAsStringAsync().ConfigureAwait(false);
// }
//}
img.Mutate(x => x.DrawImage(pointImg, new(pointx - 10, pointy - 10), new GraphicsOptions()));
}
var imgStream = new MemoryStream();
img.SaveAsPng(imgStream);
return imgStream;
//using (var byteContent = new ByteArrayContent(imgStream.ToArray()))
//{
// http.AddFakeHeaders();
// using (var reponse = await http.PutAsync("https://transfer.sh/img.png", byteContent).ConfigureAwait(false))
// {
// url = await reponse.Content.ReadAsStringAsync().ConfigureAwait(false);
// }
//}
}
catch (Exception ex)
{

View File

@@ -51,12 +51,10 @@ public class PollRunner
}
finally { _locker.Release(); }
await using (var uow = _db.GetDbContext())
{
var trackedPoll = uow.Poll.FirstOrDefault(x => x.Id == Poll.Id);
trackedPoll.Votes.Add(voteObj);
uow.SaveChanges();
}
await using var uow = _db.GetDbContext();
var trackedPoll = uow.Poll.FirstOrDefault(x => x.Id == Poll.Id);
trackedPoll.Votes.Add(voteObj);
uow.SaveChanges();
return true;
}

View File

@@ -17,7 +17,7 @@ public class TicTacToe
public int TurnTimer { get; set; } = 15;
}
enum Phase
private enum Phase
{
Starting,
Started,

View File

@@ -57,24 +57,22 @@ public partial class Games : NadekoModule<GamesService>
return;
}
await using (var imgStream = new MemoryStream())
await using var imgStream = new MemoryStream();
lock (gr)
{
lock (gr)
{
originalStream.Position = 0;
originalStream.CopyTo(imgStream);
}
imgStream.Position = 0;
await ctx.Channel.SendFileAsync(stream: imgStream,
filename: $"girl_{usr}.png",
text: Format.Bold($"{ctx.User.Mention} Girl Rating For {usr}"),
embed: _eb.Create()
.WithOkColor()
.AddField("Hot", gr.Hot.ToString("F2"), true)
.AddField("Crazy", gr.Crazy.ToString("F2"), true)
.AddField("Advice", gr.Advice, false)
.Build()).ConfigureAwait(false);
originalStream.Position = 0;
originalStream.CopyTo(imgStream);
}
imgStream.Position = 0;
await ctx.Channel.SendFileAsync(stream: imgStream,
filename: $"girl_{usr}.png",
text: Format.Bold($"{ctx.User.Mention} Girl Rating For {usr}"),
embed: _eb.Create()
.WithOkColor()
.AddField("Hot", gr.Hot.ToString("F2"), true)
.AddField("Crazy", gr.Crazy.ToString("F2"), true)
.AddField("Advice", gr.Advice, false)
.Build()).ConfigureAwait(false);
}
private double NextDouble(double x, double y)

View File

@@ -76,11 +76,9 @@ public class GamesService : INService
private async Task<RatingTexts> GetRatingTexts()
{
using (var http = _httpFactory.CreateClient())
{
var text = await http.GetStringAsync("https://nadeko-pictures.nyc3.digitaloceanspaces.com/other/rategirl/rates.json");
return JsonConvert.DeserializeObject<RatingTexts>(text);
}
using var http = _httpFactory.CreateClient();
var text = await http.GetStringAsync("https://nadeko-pictures.nyc3.digitaloceanspaces.com/other/rategirl/rates.json");
return JsonConvert.DeserializeObject<RatingTexts>(text);
}
public void AddTypingArticle(IUser user, string text)

View File

@@ -22,17 +22,15 @@ public class PollService : IEarlyBehavior
_strs = strs;
_eb = eb;
using (var uow = db.GetDbContext())
{
ActivePolls = uow.Poll.GetAllPolls()
.ToDictionary(x => x.GuildId, x =>
{
var pr = new PollRunner(db, x);
pr.OnVoted += Pr_OnVoted;
return pr;
})
.ToConcurrent();
}
using var uow = db.GetDbContext();
ActivePolls = uow.Poll.GetAllPolls()
.ToDictionary(x => x.GuildId, x =>
{
var pr = new PollRunner(db, x);
pr.OnVoted += Pr_OnVoted;
return pr;
})
.ToConcurrent();
}
public Poll CreatePoll(ulong guildId, ulong channelId, string input)