Advertisement
Guest User

Commands.cs

a guest
Apr 29th, 2021
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.39 KB | None | 0 0
  1. using Discord.Commands;
  2. using Microsoft.Data.Sqlite;
  3. using System;
  4. using System.Threading.Tasks;
  5.  
  6. namespace PrayerBot
  7. {
  8.     public class Commands : ModuleBase<SocketCommandContext>
  9.     {
  10.         [Command("ping")]
  11.         private async Task Ping()
  12.         {
  13.             await ReplyAsync("Pong! 🏓 **" + Program._client.Latency + "ms**");
  14.         }
  15.  
  16.         [Command("help")]
  17.         private async Task Help()
  18.         {
  19.             string returnStr = "Worley Byrd Help\n";
  20.             returnStr += "~addprayer prayer - this adds a prayer\n";
  21.             returnStr += "~viewprayers - view prayers that haven't been answered\n";
  22.             returnStr += "~answerprayer id# - mark prayer# as answered\n";
  23.  
  24.             await ReplyAsync(returnStr);
  25.         }
  26.  
  27.         void setKeyValue(string name, string value)
  28.         {
  29.             string keyQuery = $"select value from keys where name='{name}'";
  30.             string strValue = "";
  31.  
  32.             using (var connection = new SqliteConnection("Data Source=prayers.s3db"))
  33.             {
  34.                 connection.Open();
  35.  
  36.                 var command = connection.CreateCommand();
  37.                 command.CommandText = keyQuery;
  38.  
  39.                 using (var reader = command.ExecuteReader())
  40.                 {
  41.                     while (reader.Read())
  42.                     {
  43.                         strValue = reader.GetString(0);
  44.                     }
  45.                 }
  46.             }
  47.  
  48.             if (String.IsNullOrEmpty(strValue))
  49.             {
  50.                 //insert
  51.                 string insertQuery = $"insert into keys(name,value) values('{name}','{value}')";
  52.  
  53.  
  54.                 using (var connection = new SqliteConnection("Data Source=prayers.s3db"))
  55.                 {
  56.                     connection.Open();
  57.  
  58.                     var cmd = connection.CreateCommand();
  59.                     cmd.CommandText = insertQuery;
  60.  
  61.                     cmd.ExecuteNonQuery();
  62.                 }
  63.             }
  64.             else
  65.             {
  66.                 //update
  67.                 string updateQuery = $"update keys set value='{value}' where name='{name}'";
  68.  
  69.                 using (var connection = new SqliteConnection("Data Source=prayers.s3db"))
  70.                 {
  71.                     connection.Open();
  72.  
  73.                     var cmd = connection.CreateCommand();
  74.                     cmd.CommandText = updateQuery;
  75.  
  76.                     cmd.ExecuteNonQuery();
  77.                 }
  78.             }
  79.         }
  80.  
  81.         [Command("serverinfo")]
  82.         public async Task Serverinfo()
  83.         {
  84.             await Context.Channel.SendMessageAsync($"This Discord server's name is {Context.Guild}");
  85.         }
  86.  
  87.         [Command("posttochannel")]
  88.         [Summary("Which channel to post prayers to")]
  89.         public Task SetPostToChannel([Remainder][Summary("Set to this channel")] string channel)
  90.         {
  91.             string serverName = $"{Context.Guild}";
  92.  
  93.             setKeyValue($"posttochannel.{serverName}", channel);
  94.  
  95.             return ReplyAsync("Post Channel set to " + channel);
  96.         }
  97.  
  98.  
  99.         [Command("addprayer")]
  100.         [Summary("Add's a prayer to the prayer DB")]
  101.         public Task AddPrayerAsync([Remainder][Summary("The prayer to add")] string prayer)
  102.         {
  103.             string insertPrayer = string.Format("insert into prayers(prayer) values('{0}')", prayer);
  104.  
  105.             using (var connection = new SqliteConnection("Data Source=prayers.s3db"))
  106.             {
  107.                 connection.Open();
  108.  
  109.                 var command = connection.CreateCommand();
  110.                 command.CommandText = insertPrayer;
  111.  
  112.                 command.ExecuteNonQuery();
  113.             }
  114.  
  115.             return ReplyAsync("Prayer added!");
  116.         }
  117.  
  118.         [Command("viewprayers")]
  119.         [Summary("Add's a prayer to the prayer DB")]
  120.         public Task ListPrayersAsync()
  121.         {
  122.             string selectPrayers = string.Format("select * from prayers where answered=0");
  123.  
  124.             string resultString = "";
  125.  
  126.             using (var connection = new SqliteConnection("Data Source=prayers.s3db"))
  127.             {
  128.                 connection.Open();
  129.  
  130.                 var command = connection.CreateCommand();
  131.                 command.CommandText = selectPrayers;
  132.  
  133.                 using (var reader = command.ExecuteReader())
  134.                 {
  135.                     while(reader.Read())
  136.                     {
  137.                         var id = reader.GetString(0);
  138.                         var prayer = reader.GetString(1);
  139.  
  140.                         resultString += String.Format("Prayer {0}: {1}\n", id, prayer);
  141.                     }
  142.                 }
  143.             }
  144.  
  145.             return ReplyAsync(resultString);
  146.         }
  147.  
  148.         [Command("answerprayer")]
  149.         [Summary("Add's a prayer to the prayer DB")]
  150.         public Task AnswerPrayerAsync([Remainder][Summary("This prayer has been answered")] string id)
  151.         {
  152.             string updatePrayer = string.Format("update prayers set answered=1 where id=" + id);
  153.  
  154.             using (var connection = new SqliteConnection("Data Source=prayers.s3db"))
  155.             {
  156.                 connection.Open();
  157.  
  158.                 var command = connection.CreateCommand();
  159.                 command.CommandText = updatePrayer;
  160.  
  161.                 command.ExecuteNonQuery();
  162.             }
  163.  
  164.             return ReplyAsync(String.Format("Prayer {0} has been answered\n",id));
  165.         }
  166.  
  167.     }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement