Hello guys I have simple discord bot on server and have a music bot. My simple bot just open txt file with links on music and send command in chat m!play link. But music bot doesn't see commands which are sent by my bot.
Music bot Jockie music
import discord
from random import randint
from conf import TOKEN
class DSClient(discord.Client):
async def on_ready(self):
print(self.guilds)
async def on_message(self, message):
if message.content == "Грязюка":
with open("songs.txt", "rt") as f:
tracks = [song.strip().split() for song in f.readlines()]
for _ in range(len(tracks)):
index = randint(0, len(tracks) - 1)
track = tracks.pop(index)
print(track)
await message.channel.send(f"m!play {track[0]}")
client = DSClient(intents=discord.Intents.default().all())
client.run(TOKEN)
my code
if the bot you want to give commands is not from you you can't fix this ... because they set it in their code so that is will only respond to user accounts.
If the Bot you want to give the command (music bot) is from you, you need to go to the code and make the command triggered by an on_message event, because the build in command handler from discord.py will only listen to user messages and ignore commands send by other bots
#bot.listen
async def on_message(message):
if message.content.startswith('m!play'):
await play_song(song_name=message.content.split(' ')[1])
the play_song function is here the async function that will play the song and I just passed here an example parameter.
Related
I have a discord bot with automod and XP system. I have made discord bots before so I know how to make a command, but now it does not seem to work and I don't know why.
When I say a curse it deletes my message and everything there works fine so the on_message works, but those commands don't work. It does not give any error when I type a command, but the but doesn't respond.
Any help would be appreciated.
Here is my code:
import discord
from discord.ext import commands
# Set up the client and command prefix
intents = discord.Intents.all() # Enable all intents
client = commands.Bot(prefix='$', intents=intents)
#client.event
async def on_ready():
print('Bot ready')
#client.command()
async def ping(ctx):
print("ping")
await ctx.send('pong')
#client.event
async def on_message(message):
# Ignore messages sent by the bot
if message.author == client.user:
return
# Do some XP things
...
# AutoMod things
...
# Process any commands that are sent by the user
await client.process_commands(message)
print("processing commands")
#client.command()
async def leaderboard(ctx):
# do something
...
# Start the bot
client.run('TOKEN')
Also all intents are enabled in discord developer portal
So what I expected to happen is the program would first print Bot ready. Then I would write $ping to discord and the program would print processing commands and then print pong and say pong in discord.
What actually happened is that the program Printed Bot ready. Then I wrote $ping, the program prints processing commands. And then nothing else happens
I am trying to build a Discord bot; which tells you about the weather condition. But no matter what I did I was unable to execute even the simplest commands such as:
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix="!")
#bot.command()
async def test(ctx, arg):
await ctx.send(arg)
client = discord.Client()
client.run(token)
I just do not get it on Discord side I enter the command "!test hello" to the chat screen and nothing happens. Please help thanks !
The problem is, that you first define a bot using bot = commands.Bot(command_prefix="!") then add the command to the bot, but then create a new client using client = discord.Client() and then run the client.
The problem here is that, you never run the bot, which has the command, so instead of
client = discord.Client()
client.run(token)
use
bot.run(token)
So basically I'm doing something with a discord bot, and I got stuck in this.
let's say this is callBot_and_sendmessage function
def callBot_and_sendmessage(message):
#wake the bot
#client.event
async def on_ready():
await client.change_presence(activity = discord.Game(name='Do !help'))
#send the message
#client.event
async def on_message(message):
channel = client.get_channel(channelId)
await channel.send(message)
and my code is like this
message = input('Type a message: ')
callBot_and_sendmessage(message)
saveMessage(message)
the function saveMessage
def saveMessage(message):
with open(file, 'w') as fileData:
fileData.write(message)
My program won't call the saveMessage Function
so I need a way to stop the callBot_and_sendmessage aka the bot from running after sending the message
Can anyone help me with that ?
You can make the bot log out by using client.close(). Alternatively you can put exit(0) at the end (after the channel.send), so the script stops running after the message has been sent.
I made a simple discord.py bot that reacts whenver it is PM'd. However, if someone messages it while my bot is offline, it will not react. How can i make it display all messages received while it was online on startup?
Current code:
import discord
from discord.ext import commands
import asyncio
client = commands.Bot(command_prefix='.')
#client.event
async def on_message(message):
if message.guild is None and message.author != client.user:
send_this_console=f'{message.author}: \n{message.content}\n'
print(send_this_console)
await client.process_commands(message)
You'll need to use message history and track the last message sent.
To get when a message was created:
lastMessageTimestamp = message.created_at
You'll need to store this in whatever way you want when your bot is offline (for example with pickle) and do this:
async for message in user.history(after = lastMessageTimestamp):
print(message.content)
how do I make a a logger bot in discord.py which saves conversation into a text file.
So for example the bot saves all chats in a folder called "chatlogs" and in discord Server A every time someone says something that the bot can see, the bot logs it in a file called ServerA.txt and when Server B adds my bot, it generates a file called ServerB.txt and saves all Server B conversations in there.
In an on_message event, open the file in append mode and write the latest message.
from discord.ext import commands
bot = commands.Bot(command_prefix='!')
#bot.event
async def on_message(message):
guild = message.guild
if guild:
path = "chatlogs/{}.txt".format(guild.id)
with open(path, 'a+') as f:
print("{0.timestamp} : {0.author.name} : {0.content}".format(message), file=f)
await bot.process_commands(message)
bot.run("token")