How to set up Discord Notifications from Perforce!
So you have your Perforce server set up, possibly from following one of the many guides out there (https://www.youtube.com/watch?v=GsdS71tdGj8 is a recent simple one or https://blog.prahasta.com/how-i-installed-perforce-on-digitalocean/ for setting it up using DigitalOcean for hosting), you might want to be able to have Perforce post notifications in Discord when a change is submitted.
Note that this guide is for Linux, but the same principles apply to Windows. I am no expert in Linux, so I can’t promise that this is the most secure or optimal way to set it up. But it works.
Thanks to James Ives for coming up with this bot we’ll be using: https://github.com/JamesIves/perforce-commit-discord-bot
Setting the file up
Navigate to the folder you want to download the files to. In my case that’s:
$ cd /home
Now, using Git, clone the repo, which will make a new file in the directory you’re in:
$ git clone https://github.com/JamesIves/perforce-commit-discord-bot
if you type
you should now see a folder called “perforce-commit-discord-bot”.
$ cd /perforce-commit-discord-bot
If you can’t navigate to it. go “cd ..” to return to the parent directory, and go to the file from there:
$ cd /home/perforce-commit-discord-bot (in my case)
You want to edit the app.py file a bit:
$ nano app.py
Scroll to the last section, and replace
with the discord webhook URL (Found in Settings/Integrations of the Discord channel you want the notifications to be in). Make sure you have the quotation marks around it.
Technically this could be done by setting the discord webhook URL as an environment variable, but setting it in the file itself is more straightforward.
While you’re in there, you can also change the default information to be included in the Discord notification:
message.set_author(name='text at the top of the notification') (I set this to "Update Notes") message.set_footer(text='text at the bottom of the notification') (I set this to "From Perforce Server")
When you’re done, press Ctrl+X to exit, then Y to save, and Enter.
Installing/running the app
Now we need to actually be able to run the file by installing the dependencies/requirements from the requirements.txt file. While still in the “perforce-commit-discord-bot” folder, type:
$ pip3 install -r requirements.txt
If you don’t have Pip installed just run the command below (if you’re using Python 3).
$ sudo apt install python3-pip
Now that that’s all done, you should be able to run the following command to start the service:
$ python3 app.py
If it worked correctly, and you have any changes registered in your Perforce server, you should see a notification appear in Discord. The app works by running
$ p4 changes
to check for any changes in Perforce. If you can run that command successfully yourself, the bot should be able to as well.
Start service automatically
We want the service to start automatically every time the server reboots.
$ crontab -e
Select nano as your editor, and add a line to that file:
@reboot python3 /path/to/your/directory/app.py &
The & at the end will make the command run in the background and not prevent the system from booting up.
Ctrl+X to save, Y to confirm, and Enter.
Make the Perforce login not timeout
If you have set up any groups in perforce, to see them listed out type:
$ p4 groups
You want to edit the group the bot is using (whatever client is on the server).
$ p4 group dev (in my case)
This will open the config file in vim, a text editor. Go down to the Timeout field and set it to
Press Esc to get out of Input mode, then to save and exit type:
Final note for DigitalOceans’ virtual machines
On most Linux machines you should be done. Reboot the server and test it out. On one of DigitalOceans’ droplets though, you’ll find it won’t work right away, due to an “Initial dash character not allowed” error.
If you type
$ p4 info
You might see an (illegal) next to the Client name field. This is because the machine names can start with a – and the console Perforce client will set client name to P4HOST (the computer name) value if P4CLIENT (the workspace name) is not manually set.
So, we need to manually set P4CLIENT to something.
$ p4 set P4CLIENT=whatevernameyouwant
Now if you enter “p4 info” you should see the new name you set next to the Client Name field.
And Voila! You should have a functioning notification system! If you notice any errors with the guide, please let me know and I’ll do my best to make it more accurate.