Tsim Discord bot ntawm .NET Core nrog kev xa mus rau VPS server

Tsim Discord bot ntawm .NET Core nrog kev xa mus rau VPS server

Nyob zoo Khabrovites!

Niaj hnub no koj yuav pom ib tsab xov xwm uas yuav qhia koj yuav ua li cas los tsim ib tug bot siv C# ntawm .NET Core thiab yuav ua li cas khiav nws ntawm ib tug tej thaj chaw deb server.

Cov kab lus yuav muaj keeb kwm yav dhau los, theem npaj, sau cov logic thiab xa cov bot mus rau cov chaw taws teeb tswj.

Kuv vam tias tsab xov xwm no yuav pab tau ntau tus pib.

prehistory

Nws tag nrho pib ntawm ib hmo tsis tsaug zog lub caij nplooj zeeg uas kuv tau siv rau ntawm Discord server. Txij li thaum kuv nyuam qhuav koom nrog nws, kuv pib kawm nws nce thiab nqis. Tau pom cov ntawv nyeem channel "Vacancies", Kuv tau nyiam, qhib nws, thiab pom ntawm cov uas tsis nyiam kuv, cov no yog:

"Programmer (bot tsim tawm)
Yuav tsum muaj:

  • paub txog cov lus programming;
  • muaj peev xwm rau kev kawm tus kheej.

ОТСлания:

  • muaj peev xwm to taub lwm tus neeg txoj cai;
  • paub txog DISCORD functionality.

Cov Kev Pab:

  • kev txhim kho bot;
  • kev txhawb nqa thiab kev saib xyuas ntawm bot.

Koj qhov txiaj ntsig:

  • Lub sijhawm los txhawb thiab cuam tshuam rau qhov project koj nyiam;
  • Tau txais kev paub ntawm kev ua haujlwm hauv pab pawg;
  • Muaj cib fim los qhia thiab txhim kho cov txuj ci uas twb muaj lawm.


Qhov no tam sim ntawd txaus siab rau kuv. Yog lawm, lawv tsis tau them nyiaj rau txoj haujlwm no, tab sis lawv tsis tau thov kev lav phib xaub los ntawm koj, thiab nws yuav tsis muaj txiaj ntsig hauv cov ntaub ntawv. Yog li ntawd, kuv tau sau ntawv mus rau tus neeg rau zaub mov admin, thiab nws hais kom kuv sau ib lub bot uas yuav qhia cov neeg ua si cov txheeb cais hauv World of Tanks.

Npaj theem

Tsim Discord bot ntawm .NET Core nrog kev xa mus rau VPS server
Discrod
Ua ntej peb pib sau peb cov bot, peb yuav tsum tsim nws rau Discord. Koj xav tau:

  1. Nkag mus rau Discord account txuas
  2. Hauv "Cov Ntawv Thov" tab, nyem rau ntawm "Daim Ntawv Thov Tshiab" khawm thiab lub npe bot
  3. Tau txais bot token los ntawm kev nkag mus rau hauv koj lub bot thiab nrhiav "Bot" tab hauv "Settings" npe
  4. Txuag lub token nyob qhov twg

Sib cav sib ceg

Tsis tas li, koj yuav tsum tsim ib daim ntawv thov hauv Wargaming kom nkag mus rau Wargaming API. Ntawm no, ib yam nkaus, txhua yam yog yooj yim:

  1. Nkag mus rau koj tus account Wargaming los ntawm qhov txuas no
  2. Peb mus rau "Kuv Daim Ntawv Thov" thiab nyem rau ntawm "Ntxiv daim ntawv thov tshiab" khawm, muab lub npe ntawm daim ntawv thov thiab xaiv nws hom.
  3. Txuag Daim Ntawv Thov ID

software

twb muaj kev ywj pheej xaiv lawm. Qee tus neeg siv Visual Studio, ib tus neeg Rider, ib tus neeg feem ntau muaj zog, thiab sau cov lej hauv Vim (tom qab tag nrho, cov programmers tiag tiag siv cov keyboard nkaus xwb, puas yog?). Txawm li cas los xij, txhawm rau kom tsis txhob siv Discord API, koj tuaj yeem siv lub tsev qiv ntawv C# tsis raug cai "DSharpPlus". Koj tuaj yeem nruab nws los ntawm NuGet, lossis los ntawm kev tsim cov peev txheej koj tus kheej los ntawm qhov chaw cia khoom.

Rau cov neeg uas tsis paub lossis tsis nco qab yuav ua li cas rau nruab cov ntawv thov los ntawm NuGet.Cov lus qhia rau Visual Studio

  1. Mus rau tab Project - Tswj NuGet Pob;
  2. Nyem rau ntawm qhov kev tshuaj xyuas thiab hauv qhov chaw tshawb nrhiav sau "DSharpPlus";
  3. Xaiv thiab nruab lub moj khaum;
  4. TSWV YIM!

Cov theem kev npaj tiav lawm, koj tuaj yeem mus sau ntawv bot.

Sau logic

Tsim Discord bot ntawm .NET Core nrog kev xa mus rau VPS server

Peb yuav tsis xav txog tag nrho cov logic ntawm daim ntawv thov, kuv tsuas yog yuav ua haujlwm nrog kev cuam tshuam cov lus los ntawm bot, thiab yuav ua li cas ua haujlwm nrog Wargaming API.

Ua hauj lwm nrog Discord bot tshwm sim los ntawm static async Task MainTask(string[] args);
Txhawm rau hu rau qhov haujlwm no, hauv Main koj yuav tsum sau npe

MainTask(args).ConfigureAwait(false).GetAwaiter().GetResult();

Tom ntej no, koj yuav tsum pib koj lub bot:

discord = new DiscordClient(new DiscordConfiguration
{
    Token = token,
    TokenType = TokenType.Bot,
    UseInternalLogHandler = true,
    LogLevel = LogLevel.Debug
});

Qhov twg token yog koj tus bot's token.
Tom qab ntawd, dhau ntawm lambda, peb sau cov lus txib tsim nyog uas bot yuav tsum ua:

discord.MessageCreated += async e =>
{
    string message = e.Message.Content;
    if (message.StartsWith("&"))
    {
        await e.Message.RespondAsync(β€œHello, ” + e.Author.Username);
    }
};

Qhov twg e.Author.Username tau txais tus neeg siv lub npe menyuam yaus.

Txoj kev no, thaum koj xa cov lus uas pib nrog &, bot yuav tos txais koj.

Thaum kawg ntawm txoj haujlwm no, koj yuav tsum sau awaiting discord.ConnectAsync(); thiab tos Task.Delay(-1);

Qhov no yuav tso cai rau koj kom ua tiav cov lus txib hauv keeb kwm yav dhau yam tsis tas siv cov xov tseem ceeb.

Tam sim no peb yuav tsum tau nrog Wargaming API. Txhua yam yooj yim ntawm no - sau CURL thov, tau txais cov lus teb hauv daim ntawv ntawm JSON hlua, rub tawm cov ntaub ntawv tsim nyog los ntawm qhov ntawd thiab ua haujlwm rau lawv.

Ib qho piv txwv ntawm kev ua haujlwm nrog WargamingAPI

public Player FindPlayer(string searchNickname)
        {
            //https://api.worldoftanks.ru/wot/account/list/?application_id=y0ur_a@@_id_h3r3search=nickname
            urlRequest = resourceMan.GetString("url_find_player") + appID + "&search=" + searchNickname;
            Player player = null;
            string resultResponse = GetResponse(urlRequest);
            dynamic parsed = JsonConvert.DeserializeObject(resultResponse);

            string status = parsed.status;
            if (status == "ok")
            {
                int count = parsed.meta.count;
                if (count > 0)
                {
                    player = new Player
                    {
                        Nickname = parsed.data[0].nickname,
                        Id = parsed.data[0].account_id
                    };
                }
                else
                {
                    throw new PlayerNotFound("Π˜Π³Ρ€ΠΎΠΊ Π½Π΅ Π½Π°ΠΉΠ΄Π΅Π½");
                }
            }
            else
            {
                string error = parsed.error.message;
                if (error == "NOT_ENOUGH_SEARCH_LENGTH")
                {
                    throw new PlayerNotFound("ΠœΠΈΠ½ΠΈΠΌΡƒΠΌ Ρ‚Ρ€ΠΈ символа трСбуСтся");
                }
                else if (error == "INVALID_SEARCH")
                {
                    throw new PlayerNotFound("НСвСрный поиск");
                }
                else if (error == "SEARCH_NOT_SPECIFIED")
                {
                    throw new PlayerNotFound("ΠŸΡƒΡΡ‚ΠΎΠΉ Π½ΠΈΠΊΠ½Π΅ΠΉΠΌ");
                }
                else
                {
                    throw new Exception("Something went wrong.");
                }
            }

            return player;
        }

Nco ntsoov! Nws tsis raug pom zoo kom khaws tag nrho cov tokens thiab daim ntawv thov ID hauv cov ntawv ntshiab! Qhov tsawg kawg nkaus, Discord txwv cov tokens thaum lawv nkag mus rau hauv lub ntiaj teb network, thiab qhov siab tshaj plaws, bot pib siv los ntawm cov neeg tawm tsam.

Xa mus rau VPS - server

Tsim Discord bot ntawm .NET Core nrog kev xa mus rau VPS server

Thaum koj ua tiav nrog bot, nws yuav tsum tau tuav ntawm lub server uas ua haujlwm tas li 24/7. Qhov no yog vim qhov tseeb tias thaum koj daim ntawv thov ua haujlwm, bot kuj tseem ua haujlwm. Thaum koj kaw daim ntawv thov, koj tus bot kuj tsaug zog.

Ntau VPS servers muaj nyob hauv lub ntiaj teb no, ob qho tib si ntawm Windows thiab ntawm Linux, txawm li cas los xij, feem ntau, nws pheej yig dua rau host ntawm Linux.

Ntawm Discord server, kuv tau qhia vscale.io, thiab tam sim ntawd kuv tsim ib lub server virtual ntawm Ubuntu ntawm nws thiab xa cov bot. Kuv yuav tsis piav qhia txog qhov chaw no ua haujlwm li cas, tab sis yuav ncaj qha mus rau qhov chaw bot.

Ua ntej tshaj plaws, koj yuav tsum nruab qhov tsim nyog software uas yuav khiav peb cov bot sau hauv .NET Core. Yuav ua li cas tau piav qhia ntawm no.

Tom ntej no, koj yuav tsum tau xa cov bot mus rau qhov kev pabcuam Git, zoo li GitHub thiab lwm yam, thiab clone nws mus rau VPS server, lossis rub koj lub bot rau lwm txoj hauv kev. Thov nco ntsoov tias koj tsuas muaj lub console, tsis muaj GUI. Tag nrho.

Tom qab koj tau downloaded koj bot, koj yuav tsum tau khiav nws. Rau qhov no, koj xav tau:

  • Restore tag nrho cov dependencies: dotnet restore
  • Tsim daim ntawv thov: dotnet tsim lub npe_project.sln -c Tso tawm
  • Mus rau tsim DLL;
  • dotnet npe_of_file.dll

Nrog koj zoo siab! Koj lub bot tab tom khiav. Txawm li cas los xij, bot, hmoov tsis, nyob hauv lub console, thiab nws tsis yooj yim los tawm ntawm VPS server. Tsis tas li ntawd, nyob rau hauv rooj plaub ntawm tus neeg rau zaub mov rov pib dua, koj yuav tau pib lub bot hauv txoj kev tshiab. Muaj ob peb txoj hauv kev tawm ntawm qhov xwm txheej. Tag nrho cov ntawm lawv muaj feem xyuam rau lub community launch ntawm server startup:

  • Ntxiv cov ntawv sau rau /etc/init.d
  • Tsim ib qho kev pabcuam uas yuav ua haujlwm thaum pib.

Kuv tsis pom lub ntsiab lus nyob hauv lawv qhov nthuav dav, txhua yam tau piav qhia hauv Internet txaus.

tshawb pom

Kuv zoo siab kuv tau ua txoj haujlwm no. Nov yog kuv thawj qhov kev txhim kho bot, thiab kuv zoo siab tias kuv tau txais kev paub tshiab hauv C #, thiab ua haujlwm nrog Linux.

Txuas rau Discord server. Rau cov neeg uas ua si Wargaming.
Txuas mus rau qhov chaw khaws cia uas Discord bot nyob.
Txuas mus rau DSharpPlus repository.
Ua tsaug rau koj txoj kev paub!

Tau qhov twg los: www.hab.com

Ntxiv ib saib