【问题标题】:Sending message to Discord channel向 Discord 频道发送消息
【发布时间】:2024-01-20 07:50:01
【问题描述】:

我想要达到的目标:

每当外部程序执行console_app.exe时,我想在Discord频道上发送消息,所以我决定使用纯休息:

我通过https://discord.com/developers/applications创建了APP

我有 ClientId 和 ClientSecret,现在我想从代码登录并在特定频道上发送消息

我正在获取令牌,但响应如下:

{
  "access_token": "6qrZcUqja781...HBFG",
  "token_type": "Bearer",
  "expires_in": 604800,
  "scope": "identify connections"
}

但是当我尝试发送消息(函数WriteMessage)时,由于某种原因我是Unauthorized

整个代码:

private static readonly RestClient client = new RestClient("https://discord.com");

public string Perform()
{
    client.Authenticator = new HttpBasicAuthenticator(_config.ClientId, _config.ClientSecret);
    var token = GetToken();
    client.Authenticator = null;
    return WriteMessage(token);
}

private string GetToken()
{
    var signInRequest = new RestRequest("/api/v6/oauth2/token", Method.POST);
    signInRequest.AddHeader("content-type", "application/x-www-form-urlencoded");
    signInRequest.AddParameter("grant_type", "client_credentials");
    signInRequest.AddParameter("scope", "identify connections");

    var result = client.Execute<AuthResult>(signInRequest);

    return result.Data.access_token;
}

private string WriteMessage(string token)
{
    var request = new RestRequest("/api/v8/channels/.../messages", Method.POST);

    request.AddHeader("Authorization", $"Bearer {token}");
    //request.AddHeader("Authorization", $"{token}");
    
    var msg = new DiscordMessage
    {
        content = "Test",
        nonce = "78714914815023104",
        tts = false
    };

    request.AddJsonBody(msg);

    IRestResponse response = client.Execute(request);
    var content = response.Content;

    Console.WriteLine(response.StatusCode); // Unauthorized
    Console.WriteLine(response.Content);
    return content;
}

我可能做错了什么?

【问题讨论】:

  • 您是否要求超出您的范围?如果您唯一的范围是“识别连接”,我猜这可能是一个问题。
  • @sunero4 我尝试了bot identify bot 之类的范围,但仍然没有工作:/
  • @Axelly 是应用程序还是机器人? Discord API 文档将两者区分开来。您想代表用户发送消息吗?还是您想作为机器人发送消息?
  • @jandrew 我想要任何可以让我执行控制台应用程序并在频道上发送消息的东西 - 我应该尝试使用什么?机器人还是应用程序?
  • 您可以尝试使用Fiddler 查看您发送的请求。也许通过查看请求本身,您可以确定是否缺少某些内容。

标签: c# discord


【解决方案1】:

这是使用 Discord.NET 的解决方案

public DiscordWrapper(string token)
{
    _token = token;
}

public async Task SendMessage()
{
    _client = new DiscordSocketClient();

    await _client.LoginAsync(TokenType.Bot, _token);
    await _client.StartAsync();

    // this is important
    // found it here:
    // https://github.com/discord-net/Discord.Net/issues/1100
    _client.Ready += _client_Ready;

     await Task.Delay(-1);
}

private async Task _client_Ready()
{
    var guild = _client.GetGuild(7....3); // guild id

    var channel = guild.GetTextChannel(79034....63); // channel id

    await channel.SendMessageAsync("my_message");

    Environment.Exit(0);
}

【讨论】:

    【解决方案2】:

    使用这个在 github 上非常流行的 C# API 可能会更容易:https://github.com/discord-net/Discord.Net

    从头开始编写身份验证可能会很痛苦。

    值得花几分钟来尝试这个库,也许它可以正常工作。您需要阅读自述文件,因为还有 4 个您可能感兴趣的附加 nuget 包。

    【讨论】: