Skip to content

Commit

Permalink
Added Google Drive back, and /send command between bots interaction
Browse files Browse the repository at this point in the history
  • Loading branch information
outerwinnie committed Sep 4, 2024
1 parent 1c536d2 commit d23d65e
Showing 1 changed file with 46 additions and 10 deletions.
56 changes: 46 additions & 10 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using Discord;
using Discord.WebSocket;
using Discord.Commands;
using Discord.Rest;
using CsvHelper;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Drive.v3;
Expand All @@ -21,6 +20,8 @@ class Program
private static Random _random = new Random();
private static DiscordSocketClient _client;
private static ulong _channelId;
private static ulong _specificUserId; // User ID of the specific user
private static ulong _specificChannelId; // Channel ID of the specific channel
private static string _fileId;
private static string _credentialsPath;
private static TimeSpan _postTimeSpain;
Expand All @@ -32,24 +33,40 @@ static async Task Main(string[] args)
// Read environment variables
var token = Environment.GetEnvironmentVariable("DISCORD_BOT_TOKEN");
var channelIdStr = Environment.GetEnvironmentVariable("DISCORD_CHANNEL_ID");
var specificUserIdStr = Environment.GetEnvironmentVariable("SPECIFIC_USER_ID");
var specificChannelIdStr = Environment.GetEnvironmentVariable("SPECIFIC_CHANNEL_ID");
_fileId = Environment.GetEnvironmentVariable("GOOGLE_DRIVE_FILE_ID");
_credentialsPath = Environment.GetEnvironmentVariable("GOOGLE_CREDENTIALS_PATH");
var postTimeStr = Environment.GetEnvironmentVariable("POST_TIME");

// Check if token, channelId, fileId, credentialsPath, or postTime is null or empty
if (string.IsNullOrEmpty(token) || string.IsNullOrEmpty(channelIdStr) || string.IsNullOrEmpty(_fileId) || string.IsNullOrEmpty(_credentialsPath) || string.IsNullOrEmpty(postTimeStr))
if (string.IsNullOrEmpty(token) || string.IsNullOrEmpty(channelIdStr) || string.IsNullOrEmpty(_fileId) ||
string.IsNullOrEmpty(_credentialsPath) || string.IsNullOrEmpty(postTimeStr) ||
string.IsNullOrEmpty(specificUserIdStr) || string.IsNullOrEmpty(specificChannelIdStr))
{
Console.WriteLine("Environment variables are not set correctly.");
return;
}

// Parse channel ID
// Parse channel ID, specific user ID, and specific channel ID
if (!ulong.TryParse(channelIdStr, out _channelId))
{
Console.WriteLine("Invalid DISCORD_CHANNEL_ID format.");
return;
}

if (!ulong.TryParse(specificUserIdStr, out _specificUserId))
{
Console.WriteLine("Invalid SPECIFIC_USER_ID format.");
return;
}

if (!ulong.TryParse(specificChannelIdStr, out _specificChannelId))
{
Console.WriteLine("Invalid SPECIFIC_CHANNEL_ID format.");
return;
}

// Parse post time
if (!TimeSpan.TryParse(postTimeStr, out _postTimeSpain))
{
Expand All @@ -62,6 +79,7 @@ static async Task Main(string[] args)
_client.Log += Log;
_client.Ready += OnReady;
_client.InteractionCreated += HandleInteractionAsync;
_client.MessageReceived += HandleMessageReceivedAsync; // Listen for message events

// Start the bot
await _client.LoginAsync(TokenType.Bot, token);
Expand Down Expand Up @@ -109,7 +127,7 @@ private static async Task OnReady()
return;
}

// Register commands
// Register slash commands
await RegisterCommandsAsync();

// Schedule the first post
Expand Down Expand Up @@ -137,39 +155,57 @@ private static async Task HandleInteractionAsync(SocketInteraction interaction)
{
if (interaction is SocketSlashCommand command)
{
// Check if the command is "/send"
if (command.Data.Name == "send")
{
await HandleSendCommandAsync(command);
await HandleSendCommandAsync(command.Channel);
}
}
}

private static async Task HandleMessageReceivedAsync(SocketMessage message)
{
// Ensure the message is from a user and not a bot
if (message.Author.IsBot)
return;

// Check if the message is from the specific user in the specific channel
if (message.Author.Id == _specificUserId && message.Channel.Id == _specificChannelId)
{
// Check if the message content matches the trigger (e.g., "/send" command)
if (message.Content.Equals("/send", StringComparison.OrdinalIgnoreCase))
{
await HandleSendCommandAsync(message.Channel);
}
}
}

private static async Task HandleSendCommandAsync(SocketSlashCommand command)
private static async Task HandleSendCommandAsync(IMessageChannel channel)
{
if (_isImageUrlsLoaded)
{
if (_imageUrls.Count > 0)
{
int index = _random.Next(_imageUrls.Count);
string randomUrl = _imageUrls[index];
await command.RespondAsync(randomUrl);
await channel.SendMessageAsync(randomUrl);
}
else
{
await command.RespondAsync("No URLs available.");
await channel.SendMessageAsync("No URLs available.");
}
}
else
{
await command.RespondAsync("The bot is still loading data. Please try again later.");
await channel.SendMessageAsync("The bot is still loading data. Please try again later.");
}
}

private static async Task ScheduleNextPost()
{
var nowUtc = DateTime.UtcNow;
var spainTime = TimeZoneInfo.ConvertTimeFromUtc(nowUtc, _spainTimeZone);

// Specify that nextPostTimeSpain is unspecified in terms of kind because we will convert it to a specific time zone
var nextPostTimeSpain = DateTime.SpecifyKind(DateTime.Today.Add(_postTimeSpain), DateTimeKind.Unspecified);

Expand Down

0 comments on commit d23d65e

Please sign in to comment.