I'm trying to display the last message of a discord chat in C#

85 views Asked by At

I'm trying to display the last message of a discord in C# chat. It's been a while since I started using discord bots. My bot connects well when I launch my application the nickname of the last person to send a message is displayed but the content is not displayed. yet it's in the name ? "message.Content". I'd also like to know if it's dangerous to make the bot's token public if I only allow it to read messages?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using PS3Lib;
using Discord;
using Discord.WebSocket;
using Discord.Net;
using Newtonsoft.Json.Linq;
using System.Runtime.Remoting.Contexts;
using System.CodeDom.Compiler;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace Rtm_Ps3
{

    public partial class Form1 : Form
    {

     

        
        DiscordSocketClient client = new DiscordSocketClient();
        string token = "(MY TOKEN)";
       
        bool button4Activation = false;
        

        

        public string TexteAEnvoyer { get; set; }
        private PS3API PS3 = new PS3API();
        private Random rand = new Random();
        public Form1()
        {
            InitializeComponent();
            Form2 form2 = new Form2("\n\t\t\t INFO \n\n  This is a demo RTM tool for minecraft ps3"); form2.Show();
            
                        // Connexion Discord
                        client.MessageReceived += HandleMessageReceived;
                        
                        Start();
            
        }

        
        public async Task Start()
        {
            await client.LoginAsync(TokenType.Bot, token);
            await client.StartAsync();

            // Attendez indéfiniment pour écouter les nouveaux messages
            await Task.Delay(-1);
        }
        private Task HandleMessageReceived(SocketMessage message)
        {
            Console.WriteLine(Convert.ToString(message));
            // Vérifier si le message provient du serveur souhaité
            if (message.Channel is SocketTextChannel channel && channel.Guild.Id == ulong.Parse("710245976249598032"))
            {
                // Vérifier si le message provient du salon souhaité
                if (channel.Id == ulong.Parse("1126188227775180872"))
                {
                    // Gérer le nouveau message ici
                    string msgDiscord = $"{message.Content}";

                    string guyDiscord = $"{message.Author.Username}";
                    

                    string temp = "";

                    for (int i = 0; i <4; i++)
                    {
                        
                        temp += guyDiscord[i];
                    }
                    guyDiscord = temp;

                    string txtFinalMsg = guyDiscord + "[" + msgDiscord + "]";

                    temp = "";

                    // Débogage : Afficher la valeur de msgDiscord dans la console
                    Console.WriteLine("Message Discord : " + message.ToString());

                    // Mettre à jour le TextBox avec le nouveau message
                    textBox1.Invoke((MethodInvoker)(() =>
                    {
                        textBox1.AppendText(txtFinalMsg + Environment.NewLine);
                    }));


                }
            }

            return Task.CompletedTask;
        }
        

enter image description here

i tried to convert it into a string

0

There are 0 answers