Discord Fetch Random message from a Channel [Python] - python

The bot should fetch a random message from a diffrent channel and send it to the channel where the command was made.
Example: Im in #chat and it should fetch a random message from #memes and post it in #chat where I made the command.
Heres the code I made which doesn't really work.
#client.command()
async def meme(ctx, message_id, channel_id):
guild = ctx.guild
channel = guild.get_channel(int(672740818645417984))
message = guild.fetch_message(random.choice(int(message_id)))
message = await channel.fetch_message(int(message_id))
await channel.send(message)
Currently as Error it can't get the message_id

This works.
#client.command()
async def meme(ctx):
channel = client.get_channel("channel id")
allmes = []
async for message in channel.history(limit=200):
allmes.append(message)
randoms = random.choice(allmes).content
await ctx.send(randoms)

Related

Discord Bot that dms someone when a channel is created is not working

from discord.ext import commands
import discord
bot = commands.Bot(command_prefix="!")
#bot.event
async def on_guild_channel_create(channel):
message = "This Message is sent via DM"
user = bot.get_user("My_Discord_ID")
await user.send(message)
bot.run("TOKEN")
I ran the code but nothing happened. I am not sure whats going on.
To create a private message with a user, you must use the User.create_dm() method to obtain a channel. Once you have fetched that channel, you can send content to the user.
import discord
#bot.event
async def on_guild_channel_create(channel):
user = bot.get_user("My_Discord_ID")
channel = await user.create_dm()
try: await channel.send("This Message is sent via DM")
except discord.HTTPException: print("User has DMs disabled")

How do I make a discord bot that logs chats in a specific channel?

I made a Discord botm but I can't figure out how I can have the bot send a message when something is being sent in any channel, the message would basically be [user] sent [message] in [channel]
(I am using replit.com to write the code)
import discord
class MyClient(discord.Client):
async def on_ready(self):
print('Logged on as', self.user)
#listen for message
async def on_message(self, message):
# don't respond to ourselves
if message.author == self.user:
return
else:
#choose channel you want to receive bot message
channel = self.get_channel(int: channel_id_here)
#send message
await channel.send(f'{message.author} sent "{message.content}" in {message.channel}')
client = MyClient()
client.run('token')
For find your channel id: https://docs.statbot.net/docs/faq/general/how-find-id/
you can do that by getting the channel id of the channel you want to get the messages from, and the id of the channel you want the messages to be logged in. Add the following code in the on_message function:
#id of channel to get messages from
cid1 = channel id goes here
#id of channel to log messages to
cid2 = other channel id goes here
#getting channel object from id:
logc = await client.get_channel(cid1)
if message.author == self.user:
return
if message.channel.id == cid1:
await logc.send(f'{message.author} sent {message} in {logc.name}')

How to set channel ID for multiple discord servers in python

I'm wondering how to send a message to a specific channel in discord.
Here's the code I have so far:
import discord
client = discord.Client()
#client.event
async def on_ready():
channel = client.get_channel(channelID)
await channel.send('hello')
client.run("---")
I want to have is so you run a command like !channel, and for that specific guild, the channel ID is set under the channelID variable. It has to be like this because if there are multiple servers, the admins can set where the message is sent.
So far, I found this post, but it's for javascript: Set channel id for DiscordBot for multiple servers
Here's the updated code:
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix='$')
client = discord.Client()
main_channels = {}
#bot.command()
async def channel(ctx):
guild_id = ctx.guild.id
channel_id = ctx.channel.id
main_channels[guild_id] = channel_id
await channel.send('hi')
client.run("TOKEN")
Any help is appreciated!
The command you're trying to implement is very simple; just create a dictionary that associates the guild's ID with the ID of the channel in which to send the message:
bot = commands.Bot(command_prefix = "$")
main_channels = {}
# The following command associates the ID of the guild to that of the channel in which this command is run.
#client.command()
async def channel(ctx):
guild_id = ctx.guild.id
channel_id = ctx.channel.id
main_channels[guild_id] = channel_id
# The following command sends a greeting message to the main channel set earlier for this guild.
#client.command()
async def greet(ctx):
guild_id = ctx.guild.id
channel_id = main_channels[guild_id]
channel = ctx.guild.get_channel(channel_id)
await channel.send("hi")
bot.run(token)
However, I suggest you consider using a permanent data archive, either an offline file (such as a json) or a real database, because once the bot is disconnected it is no longer possible to retrieve the data stored in that dictionary.
If you want to send the message to a specific channel, you have to do this:
import discord
client = discord.Client()
#client.event
async def on_ready():
print('Online')
#client.event
async def on_message(message):
if message.content == "!channel":
channel = client.get_channel(SPECIFIC CHANNEL ID)
await channel.send("Hello")
client.run('TOKEN')
Else, if you want your bot to response in the same channel as the message, you can do this:
import discord
client = discord.Client()
#client.event
async def on_ready():
print('Online')
#client.event
async def on_message(message):
if message.content == "!channel":
await message.channel.send("Hello")
client.run('TOKEN')

Trying to make a bot leave a server by command

I am trying to make the bot leave a server with the ID, Command !leave
I get the error 'Bot' object has no attribute 'get_server'
Here is my script:
import discord
from discord.ext import commands
client = commands.Bot(command_prefix='!')
token = TOKEN HERE
#client.command()
async def leave(ctx, ID):
toleave = client.get_server(ID)
await client.leave_server(toleave)
print("Left Server")
client.run(token)
Since, discord.py 1.1, get_server has been renamed to get_guild, and leave_server has been moved to Guild.leave. So, your code would look something like:
toleave = client.get_guild(ID)
await toleave.leave()
This should work
#commands.command()
async def leave(self, ctx, *, message=None ):
guild_id = message
guild = await self.client.get_guild(int(guild_id))
channel = guild.system_channel
await channel.send("Leaving this server due to misuse!")
await guild.leave()

Get the user voice channel id

I have this following function:
async def play_youtube_url(youtube_url):
channel = client.get_channel('VOICE_CHANNEL_ID')
if youtube_url.startswith('https://www.youtube.com/watch?v='):
voice = await client.join_voice_channel(channel)
player = await voice.create_ytdl_player(youtube_url)
player.start()
else:
return 'URL_ERROR'
My question is, how can I get the voice channel id of the user that typed the command. I know how to get the server id, but i can't find how to get the voice channel id in the documentation. Thanks!
Use command extension and context:
import discord
from discord.ext import commands
...
client = commands.Bot(command_prefix="!")
#client.command(pass_context=True)
async def play_youtube_url(self, ctx, youtube_url):
channel = ctx.message.author.voice.voice_channel
# http://discordpy.readthedocs.io/en/latest/api.html#discord.Member.voice
# http://discordpy.readthedocs.io/en/latest/api.html#discord.VoiceState.voice_channel
if youtube_url.startswith('https://www.youtube.com/watch?v='):
voice = await client.join_voice_channel(channel)
player = await voice.create_ytdl_player(youtube_url)
player.start()
else:
return 'URL_ERROR'
...
client.run("token")

Categories