简体   繁体   中英

Run a raspberry pi discord bot written in C# on linux?

正如标题所说,我正在制作一个用 C# 编写的 discord bot,我最近得到了一个树莓派,想知道我是否可以在它上面安装 Linux,然后在它上面运行机器人,即使它是用 C# 编写的?

I realise that I'm answering over a year late, but as this is the first Google result, I thought it would be helpful to write a quick guide here.

This will run the .NET Core 2.1 Discord Bot app on a Raspberry Pi running Raspbian.

Also, this is under the assumption that you don't have a monitor available for your Pi (like me).

Firstly, publish your .NET Core app to run on ARM. Using WIN+X then C open command prompt. Type

cd <Path to project>

This should be the path to the folder containing the .sln Visual Studio file. Do not include the .sln file in the command.

Next, run dotnet publish -r linux-arm , this may take a while but should eventually output the directory that it has compiled to. Note this directory down somewhere, because you'll need it later.

Now, you'll need to install PuTTY on your Windows machine. This is a tool we will use to temporarily connect to the Raspberry Pi. Download it here .

To enable PuTTY to work, take the SD Card out of your Raspberry Pi and put it into your computer. In the BOOT drive, create a file called "ssh" with no extension. It must be in the root directory of the drive.

Now put the SD Card back in the Pi and turn it on. Ensure it is connected to the internet.

Once you have installed PuTTY, run the program and there will be a "Host name (or IP address)" box. Find the IP of your Raspberry Pi by looking on your router's setting page, or, if you have a monitor, by running ifconfig on the Pi's command line interface. Enter this IP Address into the box and ensure that connection type "SSH" is selected. Click Open or press enter.

A console window should appear, prompting you to log in. Use the credentials you have set up on the Pi. If you have not changed these, the default username is pi and the password raspberry .

Next, run these commands in PuTTY in this order to install .net:

sudo apt-get -y update

sudo apt-get -y install libunwind8 gettext

wget https://dotnetcli.blob.core.windows.net/dotnet/Sdk/2.1.300-rc1-008673/dotnet-sdk-2.1.300-rc1-008673-linux-arm.tar.gz

wget https://dotnetcli.blob.core.windows.net/dotnet/aspnetcore/Runtime/2.1.0-rc1-final/aspnetcore-runtime-2.1.0-rc1-final-linux-arm.tar.gz

sudo mkdir /opt/dotnet

sudo tar -xvf dotnet-sdk-2.1.300-rc1-008673-linux-arm.tar.gz -C /opt/dotnet/

sudo tar -xvf aspnetcore-runtime-2.1.0-rc1-final-linux-arm.tar.gz -C /opt/dotnet/

sudo ln -s /opt/dotnet/dotnet /usr/local/bin

To ensure these have all worked, run dotnet --info . You should get lots of information about the .NET Core installation.

Now you must download a tool to move files from a directory on the computer to the Pi. Download pscp.exe here . Note the location you have downloaded it to.

Open command prompt again, and type cd <Location of pscp.exe> , not including pscp.exe itself in the command.

Now, run this command in command prompt to move the compiled files to the Pi.

PSCP -r "<Path to linux-arm folder>" pi@<Pi's IP Address>:/home/pi/

Ensure the " s are there, because otherwise the program will become confused if there are spaces in your file path.

Now, in PuTTY, navigate to where the file we want to run is. This will be a file with the same name as your solution (.sln) and should be located in linux-arm/publish/ . Use cd <path to file> in PuTTY. (Do not include the file itself in the command). If you ever want to see all the files and folders in the directory you are currently in, use ls -la

To prepare your program to be run, run this in PuTTY: chmod 777 ./"<File Name>"

Finally, the program can be run. Use ./"<File Name>" to run it. This will run the program only if the PuTTY window is open, but it is good to test it now and have the satisfaction of all that hard work paying off.

To make the program continue to run when the PuTTY window is closed, you must install yet more stuff. (Use CTRL+C to close the currently running program) Run, in PuTTY, sudo apt-get install screen , then run the following command to start your bot:

screen -dmS discordbot "/home/pi/linux-arm/publish/<File Name>"

The bot should now run without the PuTTY window being open.

You can make the bot start automatically on reboot by using crontab. Run crontab -e to edit it. You can add a line at the bottom which begins with @reboot followed by commands. Seperate each command with ;.

An example crontab:

@reboot cd "FileBot/Running Environment" ; screen -dmS filebot "/home/pi/FileBot/Files/publish/FileBot"

You can then use sudo reboot to test this.

Hope this helps! If you have any issues following these instructions, please contact me or my friend on Discord (230Daniel#1920 or ComputerNerd100#0001) or comment on this answer.

Answer is generally yes(look into Mono posted link below). There may be some functionality which might not be supported by standard functionality is. I have used it and I know it works

http://www.mono-project.com/docs/getting-started/mono-basics/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM