How can I make my discord bot send messages from a .txt file (python)
Question:
import discord
import os
from keep_alive import keep_alive
client = discord.Client()
@client.event
async def on_ready():
print('Logged in as {0.user}'.format(client))
@client.event
async def on_message(message):
if message.author == client.user:
return
#!test1
if message.content.startswith('!test'):
await message.channel.send("<@471653134440071168> tag test")
client.run('my token')
keep_alive()
token = os.environ.get("DISCORD_BOT_SECRET")
client.run(os.getenv('my token'))
This is my code and what I want to do is create a .txt file containing the "<@471653134440071168> tag test"
message and make my bot read it and send that message to the text channel.
PS: im new to discord bots and python
Answers:
You can create this command to send any file you want.
@client.command()
async def test(ctx):
with open('name.txt', 'r') as f: #if you have the file in another folder use the path instead of just the name
msg = f.read()
await ctx.send(msg)
If you want to have it as event do it like this
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('!test'):
with open('name.txt', 'r') as f:
msg = f.read()
await ctx.send(msg)
#await bot.process_commands(message)
Rember that you should add await client.process_commands(message)
if you add any commands to your bot in the future otherwise it will run only the first event.
Is there a version of this in java?
import discord
import os
from keep_alive import keep_alive
client = discord.Client()
@client.event
async def on_ready():
print('Logged in as {0.user}'.format(client))
@client.event
async def on_message(message):
if message.author == client.user:
return
#!test1
if message.content.startswith('!test'):
await message.channel.send("<@471653134440071168> tag test")
client.run('my token')
keep_alive()
token = os.environ.get("DISCORD_BOT_SECRET")
client.run(os.getenv('my token'))
This is my code and what I want to do is create a .txt file containing the "<@471653134440071168> tag test"
message and make my bot read it and send that message to the text channel.
PS: im new to discord bots and python
You can create this command to send any file you want.
@client.command()
async def test(ctx):
with open('name.txt', 'r') as f: #if you have the file in another folder use the path instead of just the name
msg = f.read()
await ctx.send(msg)
If you want to have it as event do it like this
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('!test'):
with open('name.txt', 'r') as f:
msg = f.read()
await ctx.send(msg)
#await bot.process_commands(message)
Rember that you should add await client.process_commands(message)
if you add any commands to your bot in the future otherwise it will run only the first event.
Is there a version of this in java?