如何解决C# Discord.Net bot - 如何每 X 小时发布一次消息?
我正在使用 discord.NET API 用 C# 编写一个 discord 机器人。
我想知道我是否可以得到一些关于如何让我的机器人每 6 小时在频道中发布一条消息的指示。
堆栈溢出所需的详细信息:
我已经设置了命令。我按照教程找到了一些代码来实现这一目标。我知道我必须使用计时器,但我不确定如何将它连接到 discord .NET api。
谢谢。
这是我目前的代码。
程序.cs https://pastebin.com/rMjY8MAE
using discord;
using discord.Commands;
using discord.WebSocket;
using Newtonsoft.Json;
using System;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using sqlitePCL;
namespace PrayerBot
{
class Program
{
static void Main(string[] args) => new Program().RunBot().GetAwaiter().GetResult();
// Creating the necessary variables
public static discordSocketClient _client;
private CommandService _commands;
private IServiceProvider _services;
private BotConfig config;
// Runbot task
public async Task RunBot()
{
_client = new discordSocketClient(); // Define _client
_commands = new CommandService(); // Define _commands
_services = new ServiceCollection() // Define _services
.AddSingleton(_client)
.AddSingleton(_commands)
.BuildServiceProvider();
config = JsonConvert.DeserializeObject<BotConfig>(File.ReadAllText("config.json"));
string bottoken = config.token; // Make a string for the token
_client.Log += Log; // Logging
await RegisterCommandsAsync(); // Call registercommands
await _client.LoginAsync(TokenType.Bot,bottoken); // Log into the bot user
await _client.StartAsync(); // Start the bot user
await _client.SetGameAsync(config.game); // Set the game the bot is playing
await Task.Delay(-1); // Delay for -1 to keep the console window opened
}
private async Task RegisterCommandsAsync()
{
_client.MessageReceived += HandleCommandAsync; // Messagerecieved
await _commands.AddModulesAsync(Assembly.GetEntryAssembly(),null); // Add module to _commands
}
private Task Log(LogMessage arg) // Logging
{
Console.WriteLine(arg); // Print the log to Console
return Task.CompletedTask; // Return with completedtask
}
private async Task HandleCommandAsync(SocketMessage arg)
{
string messageLower = arg.Content.ToLower(); // Convert the message to a Lower
var message = arg as SocketUserMessage; // Create a variable with the message as SocketUserMessage
if (message is null || message.Author.IsBot) return; // Checks if the message is empty or sent by a bot
int argumentPos = 0; // Sets the argpos to 0 (the start of the message)
if (message.HasstringPrefix(config.prefix,ref argumentPos) || message.HasMentionPrefix(_client.CurrentUser,ref argumentPos)) // If the message has the prefix at the start or starts with someone mentioning the bot
{
var context = new SocketCommandContext(_client,message); // Create a variable called context
var result = await _commands.ExecuteAsync(context,argumentPos,_services); // Create a veriable called result
if (!result.IsSuccess) // If the result is unsuccessful
{
Console.WriteLine(result.ErrorReason); // Print the error to console
await message.Channel.SendMessageAsync(result.ErrorReason); // Print the error to the channel where the error was caused (e.g "UnkNown Command")
}
}
}
}
class BotConfig
{
public string token { get; set; }
public string prefix { get; set; }
public string game { get; set; }
}
}
命令.cs https://pastebin.com/1EK3M1Fi
using discord.Commands;
using Microsoft.Data.sqlite;
using System;
using System.Threading.Tasks;
namespace PrayerBot
{
public class Commands : ModuleBase<SocketCommandContext>
{
[Command("ping")]
private async Task Ping()
{
await ReplyAsync("Pong! ? **" + Program._client.Latency + "ms**");
}
[Command("help")]
private async Task Help()
{
string returnStr = "Worley Byrd Help\n";
returnStr += "~addprayer prayer - this adds a prayer\n";
returnStr += "~viewprayers - view prayers that haven't been answered\n";
returnStr += "~answerprayer id# - mark prayer# as answered\n";
await ReplyAsync(returnStr);
}
void setkeyvalue(string name,string value)
{
string keyQuery = $"select value from keys where name='{name}'";
string strValue = "";
using (var connection = new sqliteConnection("Data Source=prayers.s3db"))
{
connection.open();
var command = connection.CreateCommand();
command.CommandText = keyQuery;
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
strValue = reader.GetString(0);
}
}
}
if (String.IsNullOrEmpty(strValue))
{
//insert
string insertQuery = $"insert into keys(name,value) values('{name}','{value}')";
using (var connection = new sqliteConnection("Data Source=prayers.s3db"))
{
connection.open();
var cmd = connection.CreateCommand();
cmd.CommandText = insertQuery;
cmd.ExecuteNonQuery();
}
}
else
{
//update
string updateQuery = $"update keys set value='{value}' where name='{name}'";
using (var connection = new sqliteConnection("Data Source=prayers.s3db"))
{
connection.open();
var cmd = connection.CreateCommand();
cmd.CommandText = updateQuery;
cmd.ExecuteNonQuery();
}
}
}
[Command("serverinfo")]
public async Task Serverinfo()
{
await Context.Channel.SendMessageAsync($"This discord server's name is {Context.Guild}");
}
[Command("posttochannel")]
[Summary("Which channel to post prayers to")]
public Task SetPosttochannel([Remainder][Summary("Set to this channel")] string channel)
{
string serverName = $"{Context.Guild}";
setkeyvalue($"posttochannel.{serverName}",channel);
return ReplyAsync("Post Channel set to " + channel);
}
[Command("addprayer")]
[Summary("Add's a prayer to the prayer DB")]
public Task AddPrayerAsync([Remainder][Summary("The prayer to add")] string prayer)
{
string insertPrayer = string.Format("insert into prayers(prayer) values('{0}')",prayer);
using (var connection = new sqliteConnection("Data Source=prayers.s3db"))
{
connection.open();
var command = connection.CreateCommand();
command.CommandText = insertPrayer;
command.ExecuteNonQuery();
}
return ReplyAsync("Prayer added!");
}
[Command("viewprayers")]
[Summary("Add's a prayer to the prayer DB")]
public Task ListPrayersAsync()
{
string selectPrayers = string.Format("select * from prayers where answered=0");
string resultString = "";
using (var connection = new sqliteConnection("Data Source=prayers.s3db"))
{
connection.open();
var command = connection.CreateCommand();
command.CommandText = selectPrayers;
using (var reader = command.ExecuteReader())
{
while(reader.Read())
{
var id = reader.GetString(0);
var prayer = reader.GetString(1);
resultString += String.Format("Prayer {0}: {1}\n",id,prayer);
}
}
}
return ReplyAsync(resultString);
}
[Command("answerprayer")]
[Summary("Add's a prayer to the prayer DB")]
public Task AnswerPrayerAsync([Remainder][Summary("This prayer has been answered")] string id)
{
string updatePrayer = string.Format("update prayers set answered=1 where id=" + id);
using (var connection = new sqliteConnection("Data Source=prayers.s3db"))
{
connection.open();
var command = connection.CreateCommand();
command.CommandText = updatePrayer;
command.ExecuteNonQuery();
}
return ReplyAsync(String.Format("Prayer {0} has been answered\n",id));
}
}
}
解决方法
您需要在代码中使用一个简单的计时器:
myTimer = new System.Timers.Timer(6 * 60 * 60 * 1000); //calculate six hours in milliseconds
myTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
myTimer.Start();
private static void OnTimedEvent(object sender,ElapsedEventArgs e)
{
YourMethod();
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。