Hello,
I have a routine that create a thread inside a forum channel and send messages in it when it detect a change from a certain url. It worked fine on the 1735 nightly but since I upgraded to the 1896 one it throw a NullReferenceException. Here my code :
public static class LangsManager
{
public static async void OnCheckLangFinished(object? _, CheckLangFinishedEventArgs e)
{
if (e.UpdatedLangsData.Count == 0)
{
return;
}
DiscordForumChannel? forum = await GetLangForumChannel();
if (forum is null)
{
return;
}
DiscordThreadChannel thread = await CreateThreadAsync(forum, e.Type, e.Language);
foreach (LangData updatedLangData in e.UpdatedLangsData)
{
Task delay = Task.Delay(1000);
await SendLangMessageAsync(thread, updatedLangData);
await delay;
}
}
private static async Task<DiscordForumChannel?> GetLangForumChannel()
{
ulong id = Bot.Config.LangForumChannelId;
if (id == 0)
{
return null;
}
try
{
DiscordChannel channel = await Bot.Client.GetChannelAsync(id);
if (channel is DiscordForumChannel forum)
{
return forum;
}
Log.Error("The given lang channel is not a forum {ChannelId}", id);
}
catch
{
Log.Error("Unknown lang forum channel {ChannelId}", id);
}
return null;
}
private static async Task<DiscordThreadChannel> CreateThreadAsync(DiscordForumChannel forum, LangType type, LangLanguage language)
{
DateTime now = DateTime.Now;
ForumPostBuilder postBuilder = new ForumPostBuilder()
.WithName($"{type} {language} {now:dd-MM-yyyy HH\\hmm}")
.WithMessage(new DiscordMessageBuilder().WithContent($"Diff des langs {Formatter.Bold(type.ToString())} de {now:HH\\hmm} le {now:dd/MM/yyyy} en {Formatter.Bold(language.ToString())}"));
DiscordForumTag? typeTag = GetDiscordForumTagByName(forum, type.ToString());
if (typeTag is not null)
{
postBuilder.AddTag(typeTag);
}
DiscordForumTag? languageTag = GetDiscordForumTagByName(forum, language.ToString());
if (languageTag is not null)
{
postBuilder.AddTag(languageTag);
}
DiscordForumPostStarter post = await forum.CreateForumPostAsync(postBuilder);
return post.Channel;
}
private static DiscordForumTag? GetDiscordForumTagByName(DiscordForumChannel forum, string name)
{
return forum.AvailableTags.FirstOrDefault(x => x.Name.Equals(name));
}
private static async Task<DiscordMessage> SendLangMessageAsync(DiscordThreadChannel thread, LangData langData)
{
DiscordMessageBuilder message = new DiscordMessageBuilder()
.WithContent($"{(langData.New ? $"{Formatter.Bold("New")} lang" : "Lang")} {Formatter.Bold(langData.Name)} version {Formatter.Bold(langData.Version.ToString())}");
string diffFilePath = langData.GetDiffFilePath();
if (!File.Exists(diffFilePath))
{
return await thread.SendMessageAsync(message);
}
using FileStream fileStream = File.OpenRead(diffFilePath);
return await thread.SendMessageAsync(message.AddFile($"{langData.Name}.as", fileStream));
}
}