Initial commit but real
This commit is contained in:
parent
9933af247e
commit
c4f8277529
20
README.md
20
README.md
|
@ -1,3 +1,19 @@
|
|||
# perl_discord_qotd
|
||||
# Perl QoTD bot for Discord
|
||||
|
||||
A Perl script to make "question of the day" type posts to Discord via webhooks
|
||||
Pulls random questions from a database and posts them using Discord webhooks.
|
||||
|
||||
## Misc info
|
||||
Requires `DBI` and `WebService::Discord::Webhook` from the CPAN. Find the `$url` var and change it to have your webhook URL. Run periodically using a cron script or similar. Has been tested on Fedora 36 & 37 as well as Rocky Linux 9.
|
||||
|
||||
**Question data written using anything other than plain latin does not work.** I plan to fix this eventually, hopefully!
|
||||
|
||||
## Database info
|
||||
Create a database from the included 'questions.sql' file.
|
||||
|
||||
- **id**: numerical question ID, autoincrements, don't touch this
|
||||
- **question**: actual question text
|
||||
- **source**: intended to be used for the name of whoever submitted the question, but can be easily altered to display e.g. the website a question was fetched from
|
||||
- **used**: indicates whether a question has been used or not, indended to be either '0' for unused or '1' for used but can be other numbers if you need (i.e. '2' which you assign to questions you want to use but not right now, and you have another tool that will switch them over to '0' at a specified time)
|
||||
- **when**: Unix timestamp indicating when the question was used, just for reference
|
||||
|
||||
By default expects a database named `questions.sqlite`. Has only been tested with SQLite but should theoretically work with other database formats that DBI supports?
|
||||
|
|
|
@ -0,0 +1,46 @@
|
|||
#!/usr/bin/perl
|
||||
|
||||
use 5.12.0;
|
||||
use utf8;
|
||||
use WebService::Discord::Webhook;
|
||||
use DBI;
|
||||
|
||||
# Config
|
||||
my $url = 'paste webhook URL here';
|
||||
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
|
||||
my @month_name = qw(January February March April May June July August September October November December);
|
||||
$year += 1900;
|
||||
my $date = "@month_name[$mon] $mday, $year";
|
||||
|
||||
# Create the webhook object and DBI object
|
||||
my $webhook = WebService::Discord::Webhook->new( $url );
|
||||
my $dbh = DBI->connect("dbi:SQLite:questions.sqlite", { AutoCommit => 1, sqlite_unicode => 1 });
|
||||
|
||||
# Fetch ID of unsent questions from DB and pass to @questions array. If no questions are left then instead send an error to Discord and close DB connection
|
||||
my @questions = @{ $dbh->selectcol_arrayref("SELECT id FROM data WHERE used = 0") };
|
||||
if (@questions == 0) {
|
||||
$webhook->execute( content => 'There are no more questions left to post — please add more or you will get this message again tomorrow!');$dbh->disconnect; die}
|
||||
else {
|
||||
# Pick one fetched ID at random and pass to $q_id
|
||||
my $q_id = $questions[rand @questions];
|
||||
# Request question text and source for selected ID and pass to $question_text and $source_text respectively
|
||||
my $question_text = $dbh->selectrow_array("SELECT question FROM data WHERE id = " . $q_id);
|
||||
my $source_text = $dbh->selectrow_array("SELECT source FROM data WHERE id = " . $q_id);
|
||||
# Mark chosen question as used and record date
|
||||
$dbh->do("UPDATE data SET used = '1','when' = '" . time . "' WHERE id = " . $q_id);
|
||||
# Set up "questions remaining" text
|
||||
my $remains;
|
||||
if ($#questions == 1) {$remains = ' question left'} else {$remains = ' questions left';};
|
||||
|
||||
# Post the message
|
||||
$webhook->execute( embed => {
|
||||
'title' => 'Question of the day for ' . $date,
|
||||
'description' => $question_text,
|
||||
'footer' => { 'text' => 'Submitted by ' . $source_text . ' • ' . $#questions . $remains },
|
||||
'color' => rand 16777215
|
||||
});
|
||||
|
||||
if ($#questions == 0) {
|
||||
$webhook->execute( content => '**You\'re out of questions!** Please add more or no question will be posted tomorrow.');
|
||||
$dbh->disconnect; die
|
||||
} else { $dbh->disconnect; die }}
|
|
@ -0,0 +1,11 @@
|
|||
BEGIN TRANSACTION;
|
||||
DROP TABLE IF EXISTS "data";
|
||||
CREATE TABLE "data" (
|
||||
"id" INTEGER NOT NULL,
|
||||
"question" TEXT NOT NULL,
|
||||
"source" TEXT NOT NULL,
|
||||
"used" NUMERIC NOT NULL,
|
||||
"when" TEXT,
|
||||
PRIMARY KEY("id" AUTOINCREMENT)
|
||||
);
|
||||
COMMIT;
|
Loading…
Reference in New Issue