Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
## CWHQ Discourse Bot
This plugin adds extra functionality to the @system user on a Discourse forum.
This plugin adds **extra** functionality to the @system user on a Discourse forum.

## Installing The Plugin
Follow this [guide](https://meta.discourse.org/t/install-plugins-in-discourse/19157) to install this plugin.

## Features
This plugin can:
- Suggest users add links to their topic if they are missing one in certain categories
- Suggest users remove links from their topic title
- Allow Helpers to close topics
- Allow Helpers to remove system message
- Allow useful links to be displayed
- <ins>Suggest</ins> users **add** **links** to their topic if they are missing one in certain categories
- <ins>Suggest</ins> users **remove** **links** from their topic title
- Allow <ins>Helpers</ins> to **close** topics
- Allow <ins>Helpers</ins> to **remove** system message
- Allow useful **links** to be **displayed**

## Contributing
All pull requests must be error free and working as intended.
**All** pull requests must be error free and working as intended.

## License
MIT
242 changes: 242 additions & 0 deletions plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,245 @@ def send_pm_to_author(author_username, topic_id, message)

end
end
after_initialize do
DiscourseEvent.on(:user_created) do |user|
system_user = Discourse.system_user
welcome_message = <<~TEXT
Hi #{user.username},

Welcome to the CWHQ Discourse Forum! We're glad to have you here. If you have any questions or need assistance, feel free to ask.

Best,
The CWHQ Team
TEXT

PostCreator.create!(
system_user,
target_user: user,
title: "Welcome to CWHQ!",
raw: welcome_message
)
end
end
after_initialize do
PROFANE_WORDS = [
"arse", "arsehead", "arsehole", "ass", "ass hole", "asshole", "bastard", "bitch", "bloody", "bollocks",
"brotherfucker", "bugger", "bullshit", "child-fucker", "Christ on a bike", "Christ on a cracker", "cock",
"cocksucker", "crap", "cunt", "dammit", "damn", "damned", "damn it", "dick", "dick-head", "dickhead",
"dumb ass", "dumb-ass", "dumbass", "dyke", "father-fucker", "fatherfucker", "frigger", "fuck", "fucker",
"fucking", "god dammit", "god damn", "goddammit", "God damn", "goddamn", "Goddamn", "goddamned",
"goddamnit", "godsdamn", "hell", "holy shit", "horseshit", "in shit", "jack-ass", "jackarse", "jackass",
"Jesus Christ", "Jesus fuck", "Jesus H. Christ", "Jesus Harold Christ", "Jesus, Mary and Joseph", "Jesus wept",
"kike", "mother fucker", "mother-fucker", "motherfucker", "nigga", "nigra", "pigfucker", "piss", "prick",
"pussy", "shit", "shit ass", "shite", "sibling fucker", "sisterfuck", "sisterfucker", "slut",
"son of a whore", "son of a bitch", "spastic", "sweet Jesus", "twat", "wanker"
]

HELP_LINKS = "
[Forum Videos](https://forum.codewizardshq.com/t/informational-videos/8662)
[Rules Of The Forum](https://forum.codewizardshq.com/t/rules-of-the-codewizardshq-community-forum/43)
[Create Good Questions And Answers](https://forum.codewizardshq.com/t/create-good-questions-and-answers/69)
[Forum Guide](https://forum.codewizardshq.com/t/forum-new-user-guide/47)
[Meet Forum Helpers](https://forum.codewizardshq.com/t/meet-the-forum-helpers/5474)
[System Documentation](https://forum.codewizardshq.com/t/system-add-on-plugin-documentation/8742)
[Understanding Trust Levels](https://blog.discourse.org/2018/06/understanding-discourse-trust-levels/)
[Forum Information Category](https://forum.codewizardshq.com/c/official/information/69)"

DiscourseEvent.on(:post_created) do |post|
next if post.user_id == -1 # Ignore system posts

# Check for profanity
if PROFANE_WORDS.any? { |word| post.raw.downcase.include?(word) }
post.flag(Discourse.system_user, PostActionType.types[:inappropriate])
send_pm("Inappropriate Content Detected", "Your post contains inappropriate language and has been flagged for review.", post.user.username)
log_command("flagged for inappropriate content", "https://forum.codewizardshq.com/t/#{post.topic_id}", post.user.username)
end

# Check for spam (repeated links or posts)
if post.raw.scan(/https?:\/\//).count > 5 # Adjust the threshold as needed
post.flag(Discourse.system_user, PostActionType.types[:spam])
send_pm("Spam Detected", "Your post appears to be spam and has been flagged for review.", post.user.username)
log_command("flagged for spam", "https://forum.codewizardshq.com/t/#{post.topic_id}", post.user.username)
end

# Custom commands
if post.raw.downcase.include?("@system help advanced")
text = "Hello @#{post.user.username}, here are some advanced resources to help you on the forum:#{HELP_LINKS}"
create_post(post.topic_id, text)
log_command("sent advanced help", "https://forum.codewizardshq.com/t/#{post.topic_id}", post.user.username)
PostDestroyer.new(Discourse.system_user, post).destroy
end
end

DiscourseEvent.on(:user_created) do |user|
system_user = Discourse.system_user
welcome_message = <<~TEXT
Hi #{user.username},

Welcome to the CWHQ Discourse Forum! We're glad to have you here. If you have any questions or need assistance, feel free to ask.

Best,
The CWHQ Team
TEXT

PostCreator.create!(
system_user,
target_user: user,
title: "Welcome to CWHQ!",
raw: welcome_message
)
end

def post_summary
popular_topics = Topic.order('views DESC').limit(5) # Top 5 most viewed topics
summary = "Here are the top discussions:\n\n"
popular_topics.each do |topic|
summary += "* [#{topic.title}](https://forum.codewizardshq.com/t/#{topic.id}) - #{topic.views} views\n"
end

create_post(11303, summary) # Replace 11303 with the topic ID where you want to post the summary
end

# Schedule the summary to post daily or weekly
Jobs::Regular.schedule_every('1d') { post_summary } # '1d' for daily, '7d' for weekly
end
# plugin.rb
# This plugin adds extra functionality to the @system user on a Discourse forum.

# MIT License

after_initialize do
# Define the onboarding tasks
ONBOARDING_TASKS = {
fill_out_profile: "Fill out your profile",
first_post: "Make your first post",
read_guidelines: "Read the community guidelines"
}

# Method to send a private message
def send_pm(title, text, user)
message = PostCreator.create!(
Discourse.system_user,
title: title,
raw: text,
archetype: Archetype.private_message,
target_usernames: user,
skip_validations: true
)
end

# Method to check user progress
def check_user_progress(user)
progress = {}
progress[:fill_out_profile] = !user.user_profile.bio_raw.blank?
progress[:first_post] = user.post_count > 0
progress[:read_guidelines] = user.custom_fields['read_guidelines'] == true
progress
end

# Method to notify user about their progress
def notify_user_about_progress(user, progress)
message = "Hi #{user.username},\n\nHere is your onboarding checklist:\n\n"
ONBOARDING_TASKS.each do |task, description|
status = progress[task] ? "✓" : "✗"
message += "#{status} #{description}\n"
end
message += "\nPlease complete these tasks to get the most out of our community.\n\nBest,\nThe CWHQ Team"
send_pm("Your Onboarding Checklist", message, user.username)
end

# Schedule progress checks and notifications
Jobs::Regular.schedule_every('1d') do
User.where(active: true).each do |user|
progress = check_user_progress(user)
notify_user_about_progress(user, progress)
end
end

# Listen for user activity to update progress
DiscourseEvent.on(:user_updated) do |user|
if user.custom_fields['read_guidelines'] == true
progress = check_user_progress(user)
notify_user_about_progress(user, progress)
end
end

DiscourseEvent.on(:post_created) do |post|
user = post.user
if user.post_count == 1
progress = check_user_progress(user)
notify_user_about_progress(user, progress)
end
end
end


# onboarding_checklist.rb
# This plugin adds extra functionality to the @system user on a Discourse forum.

# MIT License

after_initialize do
# Define the onboarding tasks
ONBOARDING_TASKS = {
fill_out_profile: "Fill out your profile",
first_post: "Make your first post",
read_guidelines: "Read the community guidelines"
}

# Method to send a private message
def send_pm(title, text, user)
message = PostCreator.create!(
Discourse.system_user,
title: title,
raw: text,
archetype: Archetype.private_message,
target_usernames: user,
skip_validations: true
)
end

# Method to check user progress
def check_user_progress(user)
progress = {}
progress[:fill_out_profile] = !user.user_profile.bio_raw.blank?
progress[:first_post] = user.post_count > 0
progress[:read_guidelines] = user.custom_fields['read_guidelines'] == true
progress
end

# Method to notify user about their progress
def notify_user_about_progress(user, progress)
message = "Hi #{user.username},\n\nHere is your onboarding checklist:\n\n"
ONBOARDING_TASKS.each do |task, description|
status = progress[task] ? "✓" : "✗"
message += "#{status} #{description}\n"
end
message += "\nPlease complete these tasks to get the most out of our community.\n\nBest,\nThe CWHQ Team"
send_pm("Your Onboarding Checklist", message, user.username)
end

# Schedule progress checks and notifications
Jobs::Regular.schedule_every('1d') do
User.where(active: true).each do |user|
progress = check_user_progress(user)
notify_user_about_progress(user, progress)
end
end

# Listen for user activity to update progress
DiscourseEvent.on(:user_updated) do |user|
if user.custom_fields['read_guidelines'] == true
progress = check_user_progress(user)
notify_user_about_progress(user, progress)
end
end

DiscourseEvent.on(:post_created) do |post|
user = post.user
if user.post_count == 1
progress = check_user_progress(user)
notify_user_about_progress(user, progress)
end
end
end