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
Discrod
Ua ntej peb pib sau peb cov bot, peb yuav tsum tsim nws rau Discord. Koj xav tau:
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
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
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: