I am creating a simple bot that interacts with a custom database. When a user joins the server in discord, the bot has to take his ID and compare it with the ID in the database. I decided to start small, just greet the user.
After writing some simple code, the bot starts up without any problems, but when a user joins the server, the bot simply doesn't respond. I tried other commands like: channel_create, the bot responds to them
P.S. In addition to this function, there are other commands in the bot code. Ruby v2.5.2
require 'pg'
require 'discordrb'
require 'dotenv/load'
bot = Discordrb::Commands::CommandBot.new(token: 'TOKEN',
client_id: 'client_id',
prefix: '!')
bot.member_join do
bot.send_message(channel_id, 'Hi!')
end
bot.run
I haven't used the discord gem before but the docs say that you need to call bot.run in the code to tell the bot to run. It looks like you don't have that line in your code example.
https://github.com/shardlab/discordrb#usage
I was able to get the bot to respond to 'Ping!' however I wasn't able to get the 'member_join' event to trigger.
Here is my code:
require 'discordrb'
bot = Discordrb::Bot.new token: 'mytoken'
puts "This bot's invite URL is #{bot.invite_url}."
puts 'Click on it to invite it to your server.'
bot.member_join do |event|
puts 'this never seems to run'
event.send_message('Hi!')
end
bot.message(content: 'Ping!') do |event|
puts 'this runs fine'
event.respond 'Pong!'
end
bot.run
My advice is to open an issue with the Gem here: https://github.com/shardlab/discordrb/issues
Related
I currently have a Slack webhook that allows me to post results from my script to Slack.
I was just wondering if it's possible to post a screenshot to Slack? I already save screenshots to my local directory as part of the test using the screen capture function. Is there anyway to send those to Slack instead?
Sample of my code:
require 'watir'
require_relative 'regression_config.rb'
require 'date'
require 'faker'
require 'slack-notifier' # SLACK
screenCapture(browser,"Origination Portal - Login",test_id) # Screen shot sent locally to drive
# Slack functions
notifier = Slack::Notifier.new "https://hooks.slack.com/services/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" # Slack webhook
single_message = ">>>>>Auto Regression was not run. Possible login / environment issue<<<<<"
notifier.ping single_message # Post to Slack
I want my bot to send a message to a specific guild channel whenever it is added or removed from any guild. How do I make it in Discord.py? I am aware of client.event but I am unsure how to use it.
My bots code (sends to channel at the top of server where it has permissions)
#client.event
async def on_guild_join(guild):
for channel in guild.text_channels:
if channel.permissions_for(guild.me).send_messages:
embedHi = discord.Embed(
title="Thanks for adding me!",
description=
f"<:impostor:774673531786625024>I am the Impostor - a bot created by Baz!<:impostor:774673531786625024>\n\n<:noice:751384305464377375>You can join my support server by running $help and you can view all of my commands here as well!<:noice:751384305464377375>\n\n<:patreon:839897502925062165> Feel free to go to https://www.patreon.com/theimpostor to gain access to cool premium commands! <:patreon:839897502925062165>\nIf you join the <:purple:839879572631453696> Hacker Plan <:purple:839879572631453696>, then you will recieve all premium commands, a special role, early access to commands and even work in progress updates!\n:partying_face:Have fun!:partying_face:\n\n\n<:ping:757276110252670986>When you added this bot, it was in version {__version__}<:ping:757276110252670986>",
url="https://www.patreon.com/theimpostor",
colour=discord.Colour.red())
embedHi.set_thumbnail(
url=
"image url"
)
embedHi.set_image(url="image url")
embedHi.set_footer(
text="© Baz - The Impostor - Among Us bot for Discord")
await channel.send(embed=embedHi)
break
#bot.event
async def on_guild_join(guild):
# send your message
Relevant docs
I'm currently programming a bot and my first goal is to make the bot welcome and say goodbye to members that join or leave, but the bot sends nothing, not even showing any error.
#client.event
async def on_member_join(member):
print(" System: A member has joined the server!")
def sprint(str):
for c in str + '\n':
sys.stdout.write(c)
sys.stdout.flush()
time.sleep(3./90)
sprint(" Bot: Oh hey, looks like "+member.name+" has joined..")
#send message to a specific channel
channel = client.get_channel(channel id from the welcome channel)
await channel.send("Hey! Welcome to the server, "+member.mention+"! I hope you read the #rules before doing anything, otherwise have fun and enjoy your stay!")
I did it last time before I recreated the script, but the on_member_join thing doesn't work, not even on_member_leave! (I'm doing the on_member_join first to check if it's working)
My alternate account rejoined my server, but the bot (except MEE6) didn't send anything.. Is there anything you guys can help me?
You'll need to enable the member intent by creating an instance of discord.Intents.default, setting Intents.members to True, and passing the instance into your commands.Bot or discord.Client constructor, like:
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(intents=intents)
You'll also need to enable the members intent in the Discord dev portal (https://discord.com/developers). Go to that url, click on your application, go to the Bot tab, scroll down to the 'privileged gateway intents' section, and enable the members intent.
Then, restart your bot, and member events should work.
(for more info, look at https://discordpy.readthedocs.io/en/latest/intents.html)
I am working with a chatbot operating on the lita ruby gem using lita-hipchat. After a response is made to lita using hipchat, lita will be able to return messages to the user who created the response through the reply method. I would like to change this pattern and be able to send a hipchat to a secondary user, essentially being able to cc or forward that same response to more than one user. Is this possible using only the Lita gem?
I am aware that sending messages through http or the hipchat gem is another option for sending messages to secondary users, but I would prefer to do this through lita.
You can do this using Robot#send_messages. For example:
def my_handler_route(response)
user2 = Lita::User.find_by_id("user2")
target = Lita::Source(user: user2)
robot.send_message(target, "This message will go to User2!")
end
This is essentially what Response#reply is doing—but with the convenience of automatically targeting the original source.
I have set up a slack bot using slack-api and the real-time-messaging api.
Here is the abbreviated setup:
client.on :message do |data|
d {data}
bot_response = BotResponse.get_bot_response(data['text'], "session_slack")
Slack.chat_postMessage channel: data['user'], text: "#{bot_response}"
end
client.start
With this version of the postMessage, the response comes from Slackbot, not my bot (named kaya).
Goal: I want to respond to come as a DM from the bot it was sent to.
When I change the channel to data['channel'], the response comes as DM from my bot kaya, but gets into an endless loop.
How do I have a non-endless loop DM response?
NOTE:
I think I see how it is happening: by selecting the bot as the "channel" the bot is responding to it's own response back to me, as if it were another user talking into the "bot's" channel. But I can't tell how else to have the response come from my bot, not slackbot.
I believe you need to include the username parameter set to the bot name per the api: https://api.slack.com/methods/chat.postMessage, or you need the as_user option.
This mixes the Web and the RealTime Messaging API. You get a message from the RealTime Messaging API then you are using the Web API to post back. The answer of including as_user: true is correct, but you should instead use the RTM API to send the message back.
Try https://github.com/dblock/slack-ruby-client instead that cleanly separates the two. Sending a message back as the bot looks like this:
client.message channel: data['channel'], text: "Hi <##{data['user']}>!"
To avoid DM loops, make sure you're not responding to commands that you emit. There're other ways, like ignoring bot messages, but it's not as reliable.