Is there any way to send a tts message in discord.py - discord.py

I have a simple discord.py bot, I was wondering if it is possible to send a message as TTY?
The code currently runs: await ctx.send("Hello World!")
Where ctx is the context passed to the command, it simply replies to the command with "Hello World"
I wanted to make this text to speech so it would speak the message out in the channel. A user can do this by typing "/tts" before their message, so I tried replacing the prior code with this:
ctx.send("/tts Hello World!")
However this simply replied with "/tts Hello World!" rather than running TTS.
So is there any way to mark a question as TTS?

Related

discord.py on_command event not functioning

I am hosting my own local bot, however it seems as 'on_command' does not get invoked when other bots in my server are used. Could this be security restrictions? Is there any way to change this or log other bot commands?
#bot.event
async def on_command(ctx):
print(ctx)
Bot is functioning as expected - nothing is printed when a different bot runs a command.
There's no such event that can detect if other bots' commands are being used. The only one that can do something like that is on_message, and you also have to code the checks yourself.
The on_command() event does exist in discord.py, but it is only invoked when a command is used on your bot.
One way to check if a message is sent by a bot is to use the on_message() event, like this:
#bot.event
async def on_message(message):
if message.author.bot:
# Message is sent by a bot!
# ...
The message.author.bot checks if the message's author is a bot.

How to pass ctx to another function when I cant get it on python discord bot

I have already done that google assistant sends message to my text channel and I want that my other bot listen only to my google Assistant bot. I have found a way a little... tricky, by changing the method process_command() to avoid the return when the id of the message is from my google assistant.
I don't think that it is a good solution, and it would only work on my computer and it is working right now, another problem is that the bot must be in a voice channel to work. So I've been thinking, on the function on_message, with startswith("whatever character") to the message.content I can filter the messages of my bot and go directly to the function, but I need the parameter ctx that I don't know how to pass it because on_message the only parameter that it has is "message".
How can I get the ctx from the function on_message? I'm open to new suggestion about how do it on another way.
Use the get_context method
ctx = await bot.get_context(message) # or `client.get_context`
Reference:
Bot.get_context

How can I send a message to every server the bot is in (discord.py)

So I want my bot to send a message in every server my bot is in but it will send a message in only a channel name like
Example:
//broadcast hello
And the bot searches for the channel name general
And the bot sends a message to that channel and it will continue sending "hello" to other servers my bot is in.
So can anyone give me an example? Because of I do not know how. So I ask here for help
The best way to do this is to loop through every server the bot is in, then loop through each channel in each server, testing if that channel's name is #general, if so, send a message to that channel. You can use bot.guilds to get a list of all servers a bot is in, then use guild.channels to get all channels in a server, then you can use channel.name to check the name of the channel. Here's the full command:
#bot.command()
#commands.has_permissions(administrator=True)
async def broadcast(ctx, message):
for guild in bot.guilds:
for channel in guild.channels:
if(channel.name == 'general'):
await channel.send(message)
bot.run(token_here)

How to get a bot to send a message as the program is shutting down

I'm using Heroku to keep my discord bot running, and everything works fine, there's just one thing I want to add, but I've no idea how I can do it. I'd like for whenever the program shuts down, or stops running, for the bot to send a message. Can anyone give me an example of how this is possible? Thx in advance for any help.
Discord.py has a on_disconnect event which, unfortunately, won't let you send a message (as the bot already disconnected), but you can print something (e.g. the time the bot disconnected) when the bot disconnects. Unfortunately, it's impossible to send a message once the bot has disconnected.

creating a slack bot using slack-api ruby gem not responding back as a DM

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.

Resources