Skip to content

Commit

Permalink
Add a comand to force the image send
Browse files Browse the repository at this point in the history
  • Loading branch information
outerwinnie committed Sep 2, 2024
1 parent b4bfde1 commit d021d15
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 21 deletions.
57 changes: 49 additions & 8 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
using System.Linq;
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using Discord.WebSocket;
using CsvHelper;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Drive.v3;
using Google.Apis.Services;
using Microsoft.Extensions.DependencyInjection;

namespace DiscordBotExample
{
Expand All @@ -18,6 +20,8 @@ class Program
private static List<string> _imageUrls;
private static Random _random = new Random();
private static DiscordSocketClient _client;
private static CommandService _commands;
private static IServiceProvider _services;
private static ulong _channelId;
private static string _fileId;
private static string _credentialsPath;
Expand All @@ -37,11 +41,6 @@ static async Task Main(string[] args)
if (string.IsNullOrEmpty(token) || string.IsNullOrEmpty(channelIdStr) || string.IsNullOrEmpty(_fileId) || string.IsNullOrEmpty(_credentialsPath) || string.IsNullOrEmpty(postTimeStr))
{
Console.WriteLine("Environment variables are not set correctly.");
Console.WriteLine($"DISCORD_BOT_TOKEN: {(string.IsNullOrEmpty(token) ? "Not set" : "Set")}");
Console.WriteLine($"DISCORD_CHANNEL_ID: {(string.IsNullOrEmpty(channelIdStr) ? "Not set" : "Set")}");
Console.WriteLine($"GOOGLE_DRIVE_FILE_ID: {(string.IsNullOrEmpty(_fileId) ? "Not set" : "Set")}");
Console.WriteLine($"GOOGLE_CREDENTIALS_PATH: {(string.IsNullOrEmpty(_credentialsPath) ? "Not set" : "Set")}");
Console.WriteLine($"POST_TIME: {(string.IsNullOrEmpty(postTimeStr) ? "Not set" : "Set")}");
return;
}

Expand All @@ -59,10 +58,17 @@ static async Task Main(string[] args)
return;
}

// Initialize the Discord client
// Initialize the Discord client and command service
_client = new DiscordSocketClient();
_commands = new CommandService();
_services = new ServiceCollection()
.AddSingleton(_client)
.AddSingleton(_commands)
.BuildServiceProvider();

_client.Log += Log;
_client.Ready += OnReady;
_client.MessageReceived += HandleCommandAsync;

// Start the bot
await _client.LoginAsync(TokenType.Bot, token);
Expand All @@ -82,6 +88,9 @@ private static async Task OnReady()
{
Console.WriteLine("Bot is connected.");

// Initialize command handling
await _commands.AddModulesAsync(typeof(Program).Assembly, _services);

// Download and process the CSV file from Google Drive
var csvData = await DownloadCsvFromGoogleDrive();

Expand Down Expand Up @@ -119,6 +128,27 @@ private static async Task OnReady()
await ScheduleNextPost();
}

private static async Task HandleCommandAsync(SocketMessage messageParam)
{
var message = messageParam as SocketUserMessage;
var context = new SocketCommandContext(_client, message);

if (message == null || message.Author.IsBot)
return;

int argPos = 0;

if (message.HasCharPrefix('/', ref argPos))
{
var result = await _commands.ExecuteAsync(context, argPos, _services);

if (!result.IsSuccess)
{
Console.WriteLine(result.ErrorReason);
}
}
}

private static async Task ScheduleNextPost()
{
var nowUtc = DateTime.UtcNow;
Expand Down Expand Up @@ -149,7 +179,7 @@ private static async Task ScheduleNextPost()
await ScheduleNextPost();
}

private static async Task<string> DownloadCsvFromGoogleDrive()
public static async Task<string> DownloadCsvFromGoogleDrive()
{
try
{
Expand Down Expand Up @@ -189,7 +219,7 @@ private static async Task<string> DownloadCsvFromGoogleDrive()
}
}

private static async Task PostRandomImageUrl()
public static async Task PostRandomImageUrl()
{
var channel = _client.GetChannel(_channelId) as IMessageChannel;

Expand All @@ -212,4 +242,15 @@ public class YourRecordClass
public string image_url { get; set; }
public string has_spoilers { get; set; }
}

// Command Module for handling commands
public class CommandModule : ModuleBase<SocketCommandContext>
{
[Command("send")]
public async Task SendRandomImage()
{
await Program.PostRandomImageUrl();
await ReplyAsync("Random image sent!");
}
}
}
1 change: 1 addition & 0 deletions Recuerdense-Bot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<PackageReference Include="CsvHelper" Version="33.0.1" />
<PackageReference Include="Discord.Net" Version="3.15.3" />
<PackageReference Include="Google.Apis.Drive.v3" Version="1.68.0.3508" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="System.Threading.Tasks" Version="4.3.0" />
</ItemGroup>

Expand Down
Binary file modified obj/Debug/net7.0/Recuerdense-Bot.assets.cache
Binary file not shown.
Binary file modified obj/Debug/net7.0/Recuerdense-Bot.csproj.AssemblyReference.cache
Binary file not shown.
4 changes: 4 additions & 0 deletions obj/Recuerdense-Bot.csproj.nuget.dgspec.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
"target": "Package",
"version": "[1.68.0.3508, )"
},
"Microsoft.Extensions.DependencyInjection": {
"target": "Package",
"version": "[8.0.0, )"
},
"System.Threading.Tasks": {
"target": "Package",
"version": "[4.3.0, )"
Expand Down
85 changes: 74 additions & 11 deletions obj/project.assets.json
Original file line number Diff line number Diff line change
Expand Up @@ -211,20 +211,39 @@
}
}
},
"Microsoft.Extensions.DependencyInjection.Abstractions/6.0.0": {
"Microsoft.Extensions.DependencyInjection/8.0.0": {
"type": "package",
"dependencies": {
"Microsoft.Extensions.DependencyInjection.Abstractions": "8.0.0"
},
"compile": {
"lib/net6.0/Microsoft.Extensions.DependencyInjection.Abstractions.dll": {
"lib/net7.0/Microsoft.Extensions.DependencyInjection.dll": {
"related": ".xml"
}
},
"runtime": {
"lib/net6.0/Microsoft.Extensions.DependencyInjection.Abstractions.dll": {
"lib/net7.0/Microsoft.Extensions.DependencyInjection.dll": {
"related": ".xml"
}
},
"build": {
"buildTransitive/netcoreapp3.1/_._": {}
"buildTransitive/net6.0/_._": {}
}
},
"Microsoft.Extensions.DependencyInjection.Abstractions/8.0.0": {
"type": "package",
"compile": {
"lib/net7.0/Microsoft.Extensions.DependencyInjection.Abstractions.dll": {
"related": ".xml"
}
},
"runtime": {
"lib/net7.0/Microsoft.Extensions.DependencyInjection.Abstractions.dll": {
"related": ".xml"
}
},
"build": {
"buildTransitive/net6.0/_._": {}
}
},
"Microsoft.NETCore.Platforms/1.1.0": {
Expand Down Expand Up @@ -705,27 +724,66 @@
"useSharedDesignerContext.txt"
]
},
"Microsoft.Extensions.DependencyInjection.Abstractions/6.0.0": {
"sha512": "xlzi2IYREJH3/m6+lUrQlujzX8wDitm4QGnUu6kUXTQAWPuZY8i+ticFJbzfqaetLA6KR/rO6Ew/HuYD+bxifg==",
"Microsoft.Extensions.DependencyInjection/8.0.0": {
"sha512": "V8S3bsm50ig6JSyrbcJJ8bW2b9QLGouz+G1miK3UTaOWmMtFwNNNzUf4AleyDWUmTrWMLNnFSLEQtxmxgNQnNQ==",
"type": "package",
"path": "microsoft.extensions.dependencyinjection.abstractions/6.0.0",
"path": "microsoft.extensions.dependencyinjection/8.0.0",
"files": [
".nupkg.metadata",
".signature.p7s",
"Icon.png",
"LICENSE.TXT",
"PACKAGE.md",
"THIRD-PARTY-NOTICES.TXT",
"buildTransitive/net461/Microsoft.Extensions.DependencyInjection.targets",
"buildTransitive/net462/_._",
"buildTransitive/net6.0/_._",
"buildTransitive/netcoreapp2.0/Microsoft.Extensions.DependencyInjection.targets",
"lib/net462/Microsoft.Extensions.DependencyInjection.dll",
"lib/net462/Microsoft.Extensions.DependencyInjection.xml",
"lib/net6.0/Microsoft.Extensions.DependencyInjection.dll",
"lib/net6.0/Microsoft.Extensions.DependencyInjection.xml",
"lib/net7.0/Microsoft.Extensions.DependencyInjection.dll",
"lib/net7.0/Microsoft.Extensions.DependencyInjection.xml",
"lib/net8.0/Microsoft.Extensions.DependencyInjection.dll",
"lib/net8.0/Microsoft.Extensions.DependencyInjection.xml",
"lib/netstandard2.0/Microsoft.Extensions.DependencyInjection.dll",
"lib/netstandard2.0/Microsoft.Extensions.DependencyInjection.xml",
"lib/netstandard2.1/Microsoft.Extensions.DependencyInjection.dll",
"lib/netstandard2.1/Microsoft.Extensions.DependencyInjection.xml",
"microsoft.extensions.dependencyinjection.8.0.0.nupkg.sha512",
"microsoft.extensions.dependencyinjection.nuspec",
"useSharedDesignerContext.txt"
]
},
"Microsoft.Extensions.DependencyInjection.Abstractions/8.0.0": {
"sha512": "cjWrLkJXK0rs4zofsK4bSdg+jhDLTaxrkXu4gS6Y7MAlCvRyNNgwY/lJi5RDlQOnSZweHqoyvgvbdvQsRIW+hg==",
"type": "package",
"path": "microsoft.extensions.dependencyinjection.abstractions/8.0.0",
"files": [
".nupkg.metadata",
".signature.p7s",
"Icon.png",
"LICENSE.TXT",
"PACKAGE.md",
"THIRD-PARTY-NOTICES.TXT",
"buildTransitive/net461/Microsoft.Extensions.DependencyInjection.Abstractions.targets",
"buildTransitive/net462/_._",
"buildTransitive/net6.0/_._",
"buildTransitive/netcoreapp2.0/Microsoft.Extensions.DependencyInjection.Abstractions.targets",
"buildTransitive/netcoreapp3.1/_._",
"lib/net461/Microsoft.Extensions.DependencyInjection.Abstractions.dll",
"lib/net461/Microsoft.Extensions.DependencyInjection.Abstractions.xml",
"lib/net462/Microsoft.Extensions.DependencyInjection.Abstractions.dll",
"lib/net462/Microsoft.Extensions.DependencyInjection.Abstractions.xml",
"lib/net6.0/Microsoft.Extensions.DependencyInjection.Abstractions.dll",
"lib/net6.0/Microsoft.Extensions.DependencyInjection.Abstractions.xml",
"lib/net7.0/Microsoft.Extensions.DependencyInjection.Abstractions.dll",
"lib/net7.0/Microsoft.Extensions.DependencyInjection.Abstractions.xml",
"lib/net8.0/Microsoft.Extensions.DependencyInjection.Abstractions.dll",
"lib/net8.0/Microsoft.Extensions.DependencyInjection.Abstractions.xml",
"lib/netstandard2.0/Microsoft.Extensions.DependencyInjection.Abstractions.dll",
"lib/netstandard2.0/Microsoft.Extensions.DependencyInjection.Abstractions.xml",
"lib/netstandard2.1/Microsoft.Extensions.DependencyInjection.Abstractions.dll",
"lib/netstandard2.1/Microsoft.Extensions.DependencyInjection.Abstractions.xml",
"microsoft.extensions.dependencyinjection.abstractions.6.0.0.nupkg.sha512",
"microsoft.extensions.dependencyinjection.abstractions.8.0.0.nupkg.sha512",
"microsoft.extensions.dependencyinjection.abstractions.nuspec",
"useSharedDesignerContext.txt"
]
Expand Down Expand Up @@ -1179,6 +1237,7 @@
"CsvHelper >= 33.0.1",
"Discord.Net >= 3.15.3",
"Google.Apis.Drive.v3 >= 1.68.0.3508",
"Microsoft.Extensions.DependencyInjection >= 8.0.0",
"System.Threading.Tasks >= 4.3.0"
]
},
Expand Down Expand Up @@ -1231,6 +1290,10 @@
"target": "Package",
"version": "[1.68.0.3508, )"
},
"Microsoft.Extensions.DependencyInjection": {
"target": "Package",
"version": "[8.0.0, )"
},
"System.Threading.Tasks": {
"target": "Package",
"version": "[4.3.0, )"
Expand Down
5 changes: 3 additions & 2 deletions obj/project.nuget.cache
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"version": 2,
"dgSpecHash": "8L4McY806g6gaWyJ3N7rwakc1O/2wbVwtmjPlu8A5GWQBuYsZdNi3tecXUHRcjJgzaDu7Yn+CZcgGsaN4aqxeg==",
"dgSpecHash": "OqRsFgHRl7cY56bSI1IQL7iCdqXvdVoikKRr6yUyu6ZULVMINRj33//CN4Vf3HCgadJw8B+pv+bzRlgGvMU/YA==",
"success": true,
"projectFilePath": "D:\\Winnie\\Documentos\\recuerdate\\Recuerdense-Bot.csproj",
"expectedPackageFiles": [
Expand All @@ -17,7 +17,8 @@
"C:\\Users\\Winnie\\.nuget\\packages\\google.apis.core\\1.68.0\\google.apis.core.1.68.0.nupkg.sha512",
"C:\\Users\\Winnie\\.nuget\\packages\\google.apis.drive.v3\\1.68.0.3508\\google.apis.drive.v3.1.68.0.3508.nupkg.sha512",
"C:\\Users\\Winnie\\.nuget\\packages\\microsoft.bcl.asyncinterfaces\\6.0.0\\microsoft.bcl.asyncinterfaces.6.0.0.nupkg.sha512",
"C:\\Users\\Winnie\\.nuget\\packages\\microsoft.extensions.dependencyinjection.abstractions\\6.0.0\\microsoft.extensions.dependencyinjection.abstractions.6.0.0.nupkg.sha512",
"C:\\Users\\Winnie\\.nuget\\packages\\microsoft.extensions.dependencyinjection\\8.0.0\\microsoft.extensions.dependencyinjection.8.0.0.nupkg.sha512",
"C:\\Users\\Winnie\\.nuget\\packages\\microsoft.extensions.dependencyinjection.abstractions\\8.0.0\\microsoft.extensions.dependencyinjection.abstractions.8.0.0.nupkg.sha512",
"C:\\Users\\Winnie\\.nuget\\packages\\microsoft.netcore.platforms\\1.1.0\\microsoft.netcore.platforms.1.1.0.nupkg.sha512",
"C:\\Users\\Winnie\\.nuget\\packages\\microsoft.netcore.targets\\1.1.0\\microsoft.netcore.targets.1.1.0.nupkg.sha512",
"C:\\Users\\Winnie\\.nuget\\packages\\newtonsoft.json\\13.0.3\\newtonsoft.json.13.0.3.nupkg.sha512",
Expand Down

0 comments on commit d021d15

Please sign in to comment.