Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Copilot Lvl 2
Message 1 of 2

Need help with a discord bot, what the user has typed after the command and role and channel command

(Sorry about the bad title I couldnt fit everything in) Hey, so ive been making this bot for my and a friends discord server, its going well so far,
it works and connects and can execute some simple commands but I need to know how to detect what
the user has written after I ask a question for example when they do !trivia the bot does Ok, First
question have you subscribed to The Splat2ooner and Gleebie Gaming on Youtube? How do I detect what
the user said? It would also be helpful if I could change this to user and channel specific because
we are trying to do a cool event in our server and it has to be channel and role specific so yeah!
My code is this (Main code):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using Discord.WebSocket;
using Microsoft.Extensions.DependencyInjection;

namespace Trivia_Bot_Code
{
    class Program
    {
        static void Main(string[] args) => new Program().RunBotAsync().GetAwaiter().GetResult();
       
       

        private DiscordSocketClient _client;
        private CommandService _commands;
        private IServiceProvider _services;

        public async Task RunBotAsync()
        {
            _client = new DiscordSocketClient();
            _commands = new CommandService();
            _services = new ServiceCollection()
                .AddSingleton(_client)
                .AddSingleton(_commands)
                .BuildServiceProvider();

            string token = "NTk4ODU1OTAyMTE1NzkwODQ4.XScvaA.uvvmkmEeoohMwyPvPejaK1HJ3KU";

            _client.Log += _client_Log;

            await RegisterCommandsAsync();
            await _client.LoginAsync(TokenType.Bot, token);
            await _client.StartAsync();
            await Task.Delay(-1);
        }

        private Task _client_Log(LogMessage arg)
        {
            Console.WriteLine(arg);
            return Task.CompletedTask;
        }

        public async Task RegisterCommandsAsync()
        {
            _client.MessageReceived += HandleCommandAsync;
            await _commands.AddModulesAsync(Assembly.GetEntryAssembly(), _services);
           
        }

        private async Task HandleCommandAsync(SocketMessage arg)
        {
            var message = arg as SocketUserMessage;
            var context = new SocketCommandContext(_client, message);
            if (message.Author.IsBot) return;

            int argPos = 0;
            if (message.HasStringPrefix("!", ref argPos))
            {
                var result = await _commands.ExecuteAsync(context, argPos, _services);
                if (!result.IsSuccess) Console.WriteLine(result.ErrorReason);
            }
        }
    }

}

and command code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Discord.Commands;

namespace Trivia_Bot_Code.Modules
{
    public class Commands : ModuleBase<SocketCommandContext>
    {
        [Command("triviahelp")]
        public async Task triviaHelp()
        {
            await ReplyAsync("Do !Trivia for an epic trivia!");
        }

        [Command("Trivia")]
        public async Task Trivia()
        {
            await ReplyAsync("Ok, First question have you subscribed to The Splat2ooner and Gleebie Gaming on Youtube?");
           

        }
    }
}

Thanks for the help!

1 Reply
Commander Lvl 2
Message 2 of 2

Re: Need help with a discord bot, what the user has typed after the command and role and channel com

From your code, you appear to be using Discord.NET If that is the case, you might consider posting an issue to that repository.

 

However, looking through the Discord.NET samples and repository structure, it's not clear that Discord.NET has any useful support for user interactivity.

 

In contrast, the DSharpPlus project has an Interactivity assembly, and even a sample project demoing a user poll. So you might try using that library instead.

 

Please follow-up to let us know how you made out. For good karma, mark a reply as the answer if it helped!