Skip to content

Commit

Permalink
Merge pull request #7 from Hekku2/feat/generation-fixes
Browse files Browse the repository at this point in the history
Fix image sending
  • Loading branch information
Hekku2 authored Jun 28, 2024
2 parents 76d3ab7 + 49bd3a3 commit 08cb27b
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 55 deletions.
106 changes: 53 additions & 53 deletions src/Common/Discord/DiscordImagePoster.cs
Original file line number Diff line number Diff line change
@@ -1,53 +1,53 @@
using Discord;
using Discord.Rest;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace DiscordImagePoster.Common.Discord;

public class DiscordImagePoster : IDiscordImagePoster
{
private readonly ILogger<DiscordImagePoster> _logger;
private readonly DiscordConfiguration _options;

public DiscordImagePoster(ILogger<DiscordImagePoster> logger, IOptions<DiscordConfiguration> options)
{
_logger = logger;
_options = options.Value;
}

public async Task SendImage(Stream stream, string fileName, string? description)
{
var file = new FileAttachment(stream, fileName, description, false, true);
var embed = new EmbedBuilder { ImageUrl = $"attachment://{fileName}" }.Build();

using var client = await GetAuthenticatedClient();

var guild = await client.GetGuildAsync(_options.GuildId);
var channel = await guild.GetChannelAsync(_options.ChannelId);

var textChannel = await GetCorrectChannelAsync(client);
if (textChannel == null)
{
_logger.LogError("Channel {ChannelId} not found or it was not text channel.", _options.ChannelId);
return;
}
var sentMessage = await textChannel.SendFileAsync(file, fileName, false, embed: embed);
}

private async Task<DiscordRestClient> GetAuthenticatedClient()
{
var client = new DiscordRestClient();
await client.LoginAsync(TokenType.Bot, _options.Token, true);

return client;
}

private async Task<ITextChannel?> GetCorrectChannelAsync(DiscordRestClient client)
{
var guild = await client.GetGuildAsync(_options.GuildId);
var channel = await guild.GetChannelAsync(_options.ChannelId);

return channel as ITextChannel;
}
}
using Discord;
using Discord.Rest;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace DiscordImagePoster.Common.Discord;

public class DiscordImagePoster : IDiscordImagePoster
{
private readonly ILogger<DiscordImagePoster> _logger;
private readonly DiscordConfiguration _options;

public DiscordImagePoster(ILogger<DiscordImagePoster> logger, IOptions<DiscordConfiguration> options)
{
_logger = logger;
_options = options.Value;
}

public async Task SendImage(Stream stream, string fileName, string? description)
{
var file = new FileAttachment(stream, fileName, description, false, true);
var embed = new EmbedBuilder { ImageUrl = $"attachment://{fileName}" }.Build();

using var client = await GetAuthenticatedClient();

var guild = await client.GetGuildAsync(_options.GuildId);
var channel = await guild.GetChannelAsync(_options.ChannelId);

var textChannel = await GetCorrectChannelAsync(client);
if (textChannel == null)
{
_logger.LogError("Channel {ChannelId} not found or it was not text channel.", _options.ChannelId);
return;
}
var sentMessage = await textChannel.SendFileAsync(file, description ?? fileName, false, embed: embed);
}

private async Task<DiscordRestClient> GetAuthenticatedClient()
{
var client = new DiscordRestClient();
await client.LoginAsync(TokenType.Bot, _options.Token, true);

return client;
}

private async Task<ITextChannel?> GetCorrectChannelAsync(DiscordRestClient client)
{
var guild = await client.GetGuildAsync(_options.GuildId);
var channel = await guild.GetChannelAsync(_options.ChannelId);

return channel as ITextChannel;
}
}
7 changes: 5 additions & 2 deletions src/FunctionApp.Isolated/Functions/ImageSendFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,18 @@ private async Task SendRandomImage()
return;
}

var analyzationResults = await _imageAnalysisService.AnalyzeImageAsync(result.Content);
var binaryData = BinaryData.FromStream(result.Content);
var analyzationResults = await _imageAnalysisService.AnalyzeImageAsync(binaryData.ToStream());

_logger.LogInformation("Sending image {ImageName} with caption {Caption} and tags {Tags}", randomImage.Name, analyzationResults.Caption, string.Join(", ", analyzationResults.Tags));
var imageMetadata = new ImageMetadataUpdate
{
Name = randomImage.Name,
Caption = analyzationResults.Caption,
Tags = analyzationResults.Tags
};

await _discordImagePoster.SendImage(result.Content, randomImage.Name, analyzationResults.Caption);
await _discordImagePoster.SendImage(binaryData.ToStream(), randomImage.Name, analyzationResults.Caption);

await _indexService.IncreasePostingCountAndUpdateMetadataAsync(imageMetadata);
}
Expand Down

0 comments on commit 08cb27b

Please sign in to comment.