C# Discord bot made using NetCord for keeping a TikTok-style streak
1using NetCord;
2using NetCord.Services.ApplicationCommands;
3using StreakBot.Data.Entities;
4using StreakBot.Services;
5
6namespace StreakBot.Commands;
7
8public class StartCommand : ApplicationCommandModule<ApplicationCommandContext>
9{
10 private readonly StreakService _streakService;
11
12 public StartCommand(StreakService streakService)
13 {
14 _streakService = streakService;
15 }
16
17 [SlashCommand("start", "Start a daily messaging streak with another user")]
18 public async Task<string> Start(
19 [SlashCommandParameter(Name = "user", Description = "The user to start a streak with")]
20 User user)
21 {
22 var initiatorId = Context.User.Id;
23 var targetId = user.Id;
24
25 if (initiatorId == targetId)
26 {
27 return "You can't start a streak with yourself!";
28 }
29
30 if (user.IsBot)
31 {
32 return "You can't start a streak with a bot!";
33 }
34
35 Streak? success;
36 if (Context.Interaction.GuildId is null)
37 {
38 // Group DM
39 var channelId = Context.Channel.Id;
40 success = await _streakService.CreateStreakAsync(initiatorId, targetId, 0, channelId);
41 }
42 else
43 {
44 // Server
45 var serverId = Context.Interaction.GuildId.Value;
46 success = await _streakService.CreateStreakAsync(initiatorId, targetId, serverId);
47 }
48
49 return success != null ? $"Streak started between <@{initiatorId}> and <@{targetId}>! Send messages daily to keep your streak alive!" : $"Streak already started between <@{initiatorId}> and <@{targetId}>!";
50 }
51}