forked from AdarshSantoria/Discord-Dm-All-Discord-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinal bot.py
135 lines (124 loc) · 7.68 KB
/
final bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
'''
Author : Adarsh Santoria [B.Tech 2nd yr. 2022, IIT Mandi]
Project : Discord Bot
Main Idea : To fetch ids of users in a server and dm all members
Link to add bot : https://discord.com/oauth2/authorize?client_id="BOTCLIENTID"&permissions=1644971949559&scope=bot bot joining link
Note : $syntaxshow code to display syntax all functions of code
'''
import discord # impoting libraries
import json
from discord.ext import commands
import openai
import time
intents = discord.Intents.all() # defining intents necessay for latest version
intents.members = True
bot = commands.Bot(command_prefix = '$', case_insensitive = True, intents = intents) # making a bot
@bot.event # providing an event
async def on_ready(): # defining the event
print("The Bot is ready")
@bot.command(pass_context=True) # provinding a command
async def fetchdatasendmsg(ctx, guild_id, title, *, args): # defining the command
x = bot.get_guild(int(guild_id)) # find the server using the guild
members = x.members # find the members of the server
f = open('users.txt', 'a+')
for member in members: # running a for loop of members
try: # if member exists running command
embed = discord.Embed( # making a embed message (styled) and allowing some featues
color = discord.Color.red())
embed.set_author(name = 'Hi ' + member.name, icon_url = member.avatar)
embed.add_field(name = title, value = args, inline = True)
#embed.set_image(url = "give url of image") # if want to attach an image
embed.set_thumbnail(url = ctx.bot.user.avatar)
f.write(str(member.id) + '\n')
await member.send(embed = embed)
except:
print("Didn't Work") # may be member is a bot
@bot.command(pass_context = True) # provinding a command
async def fetchdata(ctx, guild_id): # defining the command
x = bot.get_guild(int(guild_id)) # find the server using the guild
members = x.members # find the members of the server
f=open('users.txt', 'a+')
for member in members: # running a for loop of members
try:
f.write(str(member.id) + '\n')
except:
print("Didn't Work") # may be member is a bot
@bot.command(pass_context = True) # provinding a command
async def txtsendmsg(ctx, title, *, args): # defining the command
f=open('users.txt')
for i in f: # running a for loop of
i = i.strip()
if i.isnumeric():
member = bot.get_user(int(i))
else:
continue
try:
embed = discord.Embed( # making a embed message (styled) and allowing some featues
color = discord.Color.red())
embed.set_author(name = 'Hi ' + member.name, icon_url = member.avatar)
embed.add_field(name = title, value = args, inline = True)
#embed.set_image(url = "give url of image") # if want to attach an image
embed.set_thumbnail(url = ctx.bot.user.avatar)
await member.send(embed = embed)
except:
print("Didn't Work") # may be member is a bot
@bot.command(pass_context = True) # provinding a command
async def txtfiledatamakeset(ctx): # defining the command
f = open('users.txt')
l = []
for i in f: # running a for loop of members
i = i.strip()
if i.isnumeric():
l.append(int(i))
l = list(set(l))
f = open('users.txt', 'w')
for i in l:
f.write(str(i) + '\n')
openai.api_key = "APIKEY"
@bot.command(pass_context = True) # provinding a command
async def announcement(ctx, guild_id, channel_id, title, *, args): # defining the command
guild = bot.get_guild(int(guild_id))
channel = guild.get_channel(int(channel_id))
if guild and channel:
embed = discord.Embed( # making a embed message (styled) and allowing some featues
color = discord.Color.red())
embed.set_author(name = 'Hi Everyone')
embed.add_field(name = title, value = args, inline = True)
#embed.set_image(url = "give url of image") # if want to attach an image
embed.set_thumbnail(url = ctx.bot.user.avatar)
await channel.send(embed = embed)
else:
print("Failed to send message. Invalid guild ID or channel ID.")
@bot.command(pass_context = True) # provinding a command
@commands.cooldown(1, 10, commands.BucketType.user) # rate limit: 1 command every 10 seconds per user
async def chatgpt(ctx, *, message): # defining the command
try:
response = openai.Completion.create(
engine ='davinci',
prompt = message,
max_tokens = 50,
n = 1,
stop = None,
temperature = 0.7
)
reply = response.choices[0].text.strip()
await ctx.send(reply)
except openai.error.RateLimitError:
await ctx.send("Oops! I'm currently experiencing high demand. Please try again later.")
@bot.command(pass_context = True) # provinding a command
async def syntaxshow(ctx): # defining the command
embed=discord.Embed(
title = "Informations about Syntax",
color = discord.Color.blue())
embed.set_author(name = 'Dear ' + ctx.author.name, icon_url = ctx.author.avatar)
embed.add_field(name = '$fetchdatasendmsg guild_id title message', value = 'This function will collect user ids and also dm all users of respective server', inline = False)
embed.add_field(name = '$fetchdata guild_id', value = 'This function will collect user ids of users of respective server', inline = False)
embed.add_field(name = '$txtsendmsg title message', value = 'This function will dm all users with th help of collected user ids', inline = False)
embed.add_field(name = '$txtfiledatamakeset', value = 'This function will do data processing on collected user ids, specially to avoid sending same message multiple times', inline = False)
embed.add_field(name = '$announcement guild_id channel_id title message', value = 'This function will post an announcement in respective channel of respective server', inline = False)
embed.add_field(name = '$chatgpt message', value = 'This function will return the output by chatgpt', inline = False)
embed.add_field(name = 'NOTE :', value = 'You can also add image to embeded message by removing comments and proving image url', inline = False)
embed.set_footer(text = 'Author : Adarsh Santoria')
await ctx.send(embed = embed)
bot.run("TOKEN") #running bot with the help of token inside
#"C:\Users\Adars\AppData\Local\Discord\app-1.0.9006\Discord.exe"